Back To SnippetsuseCopyCopy to Clipboard React Hook.use-copy.tsuse-copy.tsCopy codeimport { useCallback, useEffect, useRef, useState } from 'react'; export function useCopy(text: string, copyTimeout = 3000) { const [isCopied, setIsCopied] = useState(false); const timeout = useRef<NodeJS.Timeout | null>(null); useEffect(() => { return () => { if (timeout.current) { clearTimeout(timeout.current); } }; }, []); const copy = useCallback(() => { if (timeout.current) { clearTimeout(timeout.current); } navigator.clipboard.writeText(text); setIsCopied(true); timeout.current = setTimeout(() => { setIsCopied(false); }, copyTimeout); }, [text, copyTimeout]); return { isCopied, copy }; }