Skip to content

useClipboard

Copy to the clipboard and return a temporary copied state.

Options

NameTypeDescription
timeoutNumberthe copied state duration in ms (default is 3000)

Return Values

NameTypeDescription
copyFunctioncopy a the given text.
pastFunctionreturns the copied text from the user clipboard (require clipboard permission).
isCopiedBooleana value that stays true for a specific duration.
resetFunctioncancel the copied state.
errorUnknowncached clipboard exception.

Example Usage

tsx
import { useCopiedState } from 'react-pre-hooks';

export default function Example() {
  const { copy, isCopied, reset } = useCopiedState({ duration: 2000 });

  const text = 'a text to copy!';

  return (
    <main>
      <div onClick={reset}>{text}</div>
      <button onClick={() => copy(text)}>{isCopied ? 'copied' : 'copy'}</button>
    </main>
  );
}
import { useCopiedState } from 'react-pre-hooks';

export default function Example() {
  const { copy, isCopied, reset } = useCopiedState({ duration: 2000 });

  const text = 'a text to copy!';

  return (
    <main>
      <div onClick={reset}>{text}</div>
      <button onClick={() => copy(text)}>{isCopied ? 'copied' : 'copy'}</button>
    </main>
  );
}

Released under the MIT License.