Skip to Content
HooksuseCopyToClipboard

useCopyToClipboard

Returns a copy function plus the state of the last copy attempt. Uses the async Clipboard API (requires a secure context); when it’s unavailable, copy resolves false and records an error instead of throwing. copied flips back to false after resetDelay ms, so “Copied!” feedback needs no manual timer

npm install react-stateful-hooks

The "Copied!" label resets itself after 2 s — no manual timer needed

import { useCopyToClipboard } from 'react-stateful-hooks'; const [copy, { copied }] = useCopyToClipboard(); <button onClick={() => copy(url)}>{copied ? 'Copied!' : 'Copy link'}</button>;

Signature

const [copy, { value, error, copied }] = useCopyToClipboard(options?: { resetDelay?: number; // ms until `copied` resets; 0 = never. default: 2000 });
FieldDescription
copy(text)Async function; resolves true on success, false on failure
valueThe last successfully copied string
errorThe error from the last failed attempt, if any
copiedtrue briefly after a successful copy, then auto-resets
Last updated on