si it guaranteed that by time store.dispatch returns, the state has already changed? and if thats case, then why not return newState as a result of dispatch call?
store.dispatch( action1() );
store.dispatch( action2() );
an example I will login user in action1, then i want to fire another action that will use the LOGEDIN user information to change the state further more. so i want to make sure that action2 will not fire unless action1 has already changed the state successfully.
so, by time store.dispatch returns, is this garanteed that the state has already changed ?
example reducer:
function reducer(state, action){
// please ignore that i will mutate state, just for sake of simplicity of example.
if(action.type==='ACTION1'){
state.user_id = action.payload;
return state;
}
if(action.type==='ACTION1'){
state.value2 = state.user_id * action.whatEver;
return state;
}
return state;
}
my current protection is i use React.component to monitor changes to user_id then i fire action2, but if Redux Actions are synchronous then i can fire action2 directly after action1 and just reduce my boilerplate.
Yes, by default dispatch()
is 100% synchronous. Middleware can intercept actions and potentially delay them or modify behavior in some other asynchronous way. But,other than that, by the time dispatch
returns, it is guaranteed that the root reducer has finished running, the current state value has been swapped out, and the subscribers have been notified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With