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:
Timoteo Borgobello 2022-07-13 08:57:17 -03:00 committed by GitHub
parent 85a5959a2d
commit 2f92479fa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 (
<>