How to use callback with useState hook in react
You can use useEffect/useLayoutEffect to achieve this: More about it over here. If you are looking for an out of the box solution, check out this custom hook that works like useState but accepts as second parameter a callback function: