20

Assume I have action A which is dispatched to reducer R1 and R2. I also have smart component C which is connected to the states returned by R1 and R2

Is it possible that component C were rerendered when R1 is executed but before R2 is executed?

How if I want to rerender C only when both R1 and R2 are executed? Or is it possible to tell Redux dispatcher to dispatch the action to R2 first?

*R1 is actually the results state returned by redux normalizr and R2 is the entities state. The entities are needed to denormalize the results

Pahlevi Fikri Auliya
  • 4,157
  • 8
  • 35
  • 71

2 Answers2

32

Is it possible that component C were rerendered when R1 is executed but before R2 is executed?

No, it is not possible if you use a single store like Redux docs suggest.

In Redux, you combine reducers with combineReducers(). This means that from Redux’s point of view, there is only a single reducer, and it is synchronous. Sure, it calls your two reducers, but the Redux store only begins notifying subscribers after it gets the new state value from its root reducer, so it is guaranteed that all your reducers have already been executed by the time the views are notified.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • I had a situation similar to this where a selector was being called and causing an error because a value was undefined. That value was based on a single action being handled by 2 reducers. The select was being referenced in the mapStateToProps function of a container. Any insight into this Dan? – SomethingOn Dec 07 '16 at 19:17
1

Redux actions are dispatched synchronously, so all side effects of an action will occur immediately after the action is dispatched. This means your component will only re-render once since React doesn't re-render immediately on state change (it seems to instead defer until the next event loop or animation frame) even if you call setState multiple times.

You can define the order of dispatch by simply changing the order you're calling store.dispatch. Call R1.dispatch first if you want all side effects of R1 changing to happen first.

SimpleJ
  • 13,812
  • 13
  • 53
  • 93