Assume I have action A
which is dispatched to reducer R1
and R2
. I also have smart component C
which is connect
ed 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
Reducers are functions that take the current state and an action as arguments, and return a new state result. In other words, (state, action) => newState .
There are two main ways to initialize state for your application. The createStore method can accept an optional preloadedState value as its second argument. Reducers can also specify an initial value by looking for an incoming state argument that is undefined , and returning the value they'd like to use as a default.
Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.
One frequently asked question is whether Redux "calls all reducers" when dispatching an action. Since there really is only one root reducer function, the default answer is "no, it does not".
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.
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