I need to be able to preform an action right after I update my useReducer state with dispatch. But dispatch runs async so when I run my next piece of code, it uses the old state before dispatch was supposed to update it.
What I've tried is using useEffect(()=>{},[state]), but that just runs every time state updates. I need to preform certain actions based on conditions... not just every time state updates.
// Let's update some stuff on the server with new data!
const handleFinishOrder = () => {
dispatch({ type: "finish-order", payload: id }); // Set some fields to null
socket.emit("request-update-live-data", state); // Old state gets sent to server :(
};
I expect the state to be updated when I go emit it off to the server but the actual state that gets sent is the old state.