mikelpmc / react-use-copy-to-clipboard

React hook to copy the given text to the clipboard

Home Page:https://codesandbox.io/s/react-use-copy-to-clipboard-yw3hd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

⚛️ React copy to clipboard

React custom hook to copy the given text to the clipboard.

Compatible with Edge and IE11 as well all modern browsers.

Codesandbox demo

How to use

Basic usage

function App() {
  const {isCopied, onCopy} = useCopyToClipboard({text: "Text to copy"});

  return (
    <button type="button" onClick={onCopy}>
      Text is copied: {isCopied ? "Yes! 👍" : "Nope! 👎"}
    </button>
  );
}

Reset the isCopied success state after a given time

You can reset the isCopied flag passing an optional successDuration prop with the desired time in milliseconds.

const {isCopied, onCopy} = useCopyToClipboard({text: "Text to copy", successDuration: 2000});

About

React hook to copy the given text to the clipboard

https://codesandbox.io/s/react-use-copy-to-clipboard-yw3hd


Languages

Language:JavaScript 63.1%Language:HTML 36.9%