Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save szaranger/af6aeefe8394b10452c3f50b12d2ee47 to your computer and use it in GitHub Desktop.
Save szaranger/af6aeefe8394b10452c3f50b12d2ee47 to your computer and use it in GitHub Desktop.
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