Reputation: 727
I'm in a problem when update store of my react redux app. How can I make store update thenable? As you know, all updates of states are async, so we have to use componentDidUpdate method if I do something as a callback function of state update. But these are sometimes complicated. I'd like to make this simple, by something like making them thenable method. Does have any body any idea?
Upvotes: 0
Views: 45
Reputation: 486
The reducer function supposed to be pure so the async logic should not be there.
There are few middlewares you might be interested in:
redux-thunk allows you to have action creator that returns a function which dispatch
and getState
from store are injected into a normal action which means can delay the dispatch however you want. But it has limited power. (which you might not need more than that)
redux-saga use generators to explain all the side-effects and async operation in terms of data. It is more powerful than thunk, you can do complicated async interaction, for example, racing between multiple actions. But the downside is there will be more action you need to create and unpredictability caused by action dependencies.
redux-observable is claimed to be more sophisticated than saga. It is based on Rx observable stream and you can manipulate stream of actions with map
flatMap
filter
which similar to array's method but there are more you can use. For me, it's interface looks much nicer than saga. Some might argue against the learning curve needed for understanding Rx.
Upvotes: 2