useClipboard()
The
useClipboard() hook provides a simple interface for interacting with the Clipboard API, handling copy/paste operations, and managing UI feedback states.Import
typescript
Usage
Click "Paste Text" to see clipboard content...
Component.tsx
API
Arguments
Accepts an optional options object:
timeout– number (Optional) Duration in milliseconds for thehasCopiedstate to remain true. Defaults to 2000.
Returns
Returns an object with the following properties:
copy– (text: string) => Promise<boolean> Function to copy text to the clipboard. Returns true if successful.paste– () => Promise<string | null> Function to read text from the clipboard. Returns the text or null if failed.hasCopied– boolean True if text was recently copied. Resets automatically after the timeout.clipboardValue– string | null The last value read from the clipboard using the paste function.error– Error | null Error object if the copy or paste operation fails (e.g., permission denied).reset– () => void Function to manually reset the hasCopied state and errors.