const [counter, setCounter] = useState(0);
const doSomething = () => {
setCounter(123);
}
useEffect(() => {
console.log('Do something after counter has changed', counter);
}, [counter]);
const [state, setState] = useState(null);
const myCallbacksList = useRef([]);
const setStateWithCallback= (newState, callback) => {
setState(state);
if(callback) myCallbackList.current.push(callback)
}
useEffect(() => {
myCallbacksList.current.forEach((callback) => callback())
myCallbacksList.current = [];
}, [state]);
…
setStateWithCallback(newState, myCallback)