Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use callback in DIspatch in redux?

Tags:

reactjs

redux

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?

like image 234
PuskarShestha Avatar asked Dec 21 '25 21:12

PuskarShestha


1 Answers

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.

like image 179
Estus Flask Avatar answered Dec 24 '25 10:12

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!