I use react, redux, and redux-thunk middlewere.
My code looks like :
function thunkFunction(){
return(dispatch) => {
dispatch(toggleSmth(false));
dispatch(toggle2Smth(false));
SomeService.getSmth()
.then(response => {
dispatch(oneMoreAction);
dispatch(one2MoreAction);
}
}
}
And now I dispatch that whole thunk action after componenDidMount() in some component.
It works like that :
- dispatch(toggleSmth(false));
- dispatch(toggle2Smth(false));
- Promise to callback queue (not doing .then elements)
- } going to the close curly bracket
- Component reender
- going to .then(
- dispatching(oneMoreAction)
- Automatically component reender (WHY!?)
- back to the second dispatch -> dispatching(one2MoreAction)
- Another automatically component reender (WHY!?)
The question is :
Why once the dispatch wait for whole stack to clear. And in Promise after dispatch component is automatically reendered and after that it backs to the next Promise function element ?
In addition when I put dispatch(toggleSmth(false)) inside of promise like that :
function thunkFunction(){=
return(dispatch) => {
SomeService.getSmth()
.then(response => {
dispatch(toggleSmth(false));
dispatch(toggle2Smth(false));
dispatch(oneMoreAction);
dispatch(one2MoreAction);
}
}
}
The dispatch(toggleSmth(false))
and dispatch(toggle2Smth(false))
automatically reender component. (Not wait for clear the callback stack '}'). And after reender goes next -> to first dispatch(oneMoreAction);
Why once dispatching causing automatically reender and once you have to wait for execution of element inside of function ?