Created
November 6, 2021 22:33
-
-
Save szaranger/af6aeefe8394b10452c3f50b12d2ee47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback } from 'react'; | |
function Count() { | |
const [count, setCount] = useState(0); | |
const increase = useCallback(() => { | |
/** Avoid */ | |
// setCount(count + 1); | |
/** Use a callback instead */ | |
setCount(count => count + 1); | |
}, [count]); | |
const handleClick = () { | |
increase(); | |
increase(); | |
increase(); | |
}; | |
return ( | |
<> | |
<button onClick={handleClick}>+</button> | |
<div>Counter: {count}</div> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment