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)