Copy text to a user's clipboard.
const Demo = () => {
const [text, setText] = React.useState('');
const [state, copyToClipboard] = useCopyToClipboard();
return (
<div>
<input value={text} onChange={e => setText(e.target.value)} />
<button type="button" onClick={() => copyToClipboard(text)}>copy text</button>
{state.error
? <p>Unable to copy value: {state.error.message}</p>
: state.value && <p>Copied {state.value}</p>}
</div>
)
}const [{value, error, noUserInteraction}, copyToClipboard] = useCopyToClipboard();valueβ value that was copied to clipboard, undefined when nothing was copied.errorβ caught error when trying to copy to clipboard.noUserInteractionβ boolean indicating if user interaction was required to copy the value to clipboard to expose full API from underlyingcopy-to-clipboardlibrary.