I was looking at the documentation of redux and there was no callback field in the documentation. But there is callback in setState method. Isn't dispatch method responsible for setState function? How can i have a callback in dispatch method of redux?
The reason why React setState has callback parameter is that React state updates are asynchronous. It's needed to execute the code that depends on updated state:
this.setState(..., () => {
console.log('state updated');
});
This isn't the case for Redux. dispatch is synchronous and thus it doesn't make sense to have a callback there. It's expected that an action was dispatched right after dispatch was called:
dispatch(...);
console.log('store updated');
Isn't dispatch method responsible for setState function?
It isn't. Redux store is an alternative to React component state. It doesn't use setState internally.
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