The set function of useState hook can take a callback as an argument.

setCount(count => count + 1);

Common use of the state hook is:

const Counter = () => {
const [count, setCount] = useState(0);

return (
<button onClick={() => setCount(count + 1)}>
Increment
</button>
);
};

From a performance standpoint, it is good to wrap all callbacks with useCallback hook:

const Counter = () => {
const [count, setCount] = useState(0);
const increment = useCallback(() => setCount(count + 1), [count])
return (
<button onClick={increment}>
Increment
</button>
);
};

The increment function will be created each time the count…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store