mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
fix(example): clearTimeout and useCallback was added on CopyButton (#1080)
* fix(copyButton): clearTimeout and useCallback was added on CopyButton * fix: move clearTimeout to top of function
This commit is contained in:
parent
85a5959a2d
commit
2f92479fa9
@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useState, useCallback, useRef } from 'react'
|
||||
import { copyToClipboard } from '../utils/copy-to-clipboard'
|
||||
|
||||
/*
|
||||
@ -7,14 +7,15 @@ of a separate button component and with the added utility
|
||||
*/
|
||||
export default function CopyButton({ code, ...props }) {
|
||||
const [isCopied, setIsCopied] = useState(false)
|
||||
const timer = useRef()
|
||||
|
||||
const handleCopy = () => {
|
||||
const handleCopy = useCallback(() => {
|
||||
clearTimeout(timer.current)
|
||||
copyToClipboard(code).then(() => {
|
||||
setIsCopied(true)
|
||||
setTimeout(() => setIsCopied(false), 3000)
|
||||
timer.current = setTimeout(() => setIsCopied(false), 3000)
|
||||
})
|
||||
}
|
||||
|
||||
}, [code])
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user