Rehooks
Hooks

useClipboard

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

useClipboard

The useClipboard hook is a simple hook that provides a function to copy text to the clipboard. It is useful when you need to copy text to the clipboard in your component.

Usage

import { useClipboard } from "rehooks-ts";
 
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