Rehooks
Content

useClipboard

A hook that provides a function to copy text to the clipboard.

useClipboard

Usage

Component.tsx
import { useClipboard } from "~/hooks/useClipboard";
 
function Component() {
  const { copy, isCopied } = useClipboard();
 
  return (
    <div>
      <button onClick={() => copy("Hello, world!")}>
        Copy "Hello, world!"
      </button>
      <p>Is copied: {isCopied ? "Yes" : "No"}</p>
    </div>
  );
}

API

useClipboard

function useClipboard(): {
  copy: (text: string) => void;
  isCopied: boolean;
};

Returns a function to copy text to the clipboard and a boolean indicating whether the text has been copied.

Returns

NameTypeDescription
copy(text: string) => voidA function to copy text to the clipboard.
isCopiedbooleanA boolean indicating whether the text has been copied.

On this page