NB: This is a question, very similar to Redux; accessing other parts... but it has nothing to do with Router :( thus cannot be solved same way
When I reduce one part of the state, I "feel" like I need to access other parts as well. I admit that I just might "misfeel" the core principals of the Redux, or have flaws in my app's architecture.
My current solution would be to modify github:combineReducers.js's code:
var finalState = mapValues(finalReducers, (reducer, key) => {
var previousStateForKey = state[key]
var nextStateForKey = reducer(previousStateForKey, action)
...
}
from
var nextStateForKey = reducer(previousStateForKey, action)
to
var nextStateForKey = reducer(previousStateForKey, action, state)
which would allow me to do what I need:
function reducer(state, action, root) {
if (root.otherPart.get("isSomething")) {
return state.set("anotherThing", true);
}
return state;
}
Question is if I am on the right way to do it, or is it something that should be solved using different architectural approaches, without having a need to access one part of state from other parts?
** UPDATE 5h Dec, 2018 **
Due to relatively high interest to this question (15 up-votes atm), I am adding my own answer below, hope it helps to those who is looking for that answer.
To manage the multiple reducers we have function called combineReducers in the redux. This basically helps to combine multiple reducers into a single unit and use them. Example: Let's make a simple book list application with help of redux to understand to combine multiple reducers.
The reducer must always produce the same state given the same current state and action, it must be a pure function. A common pattern is to compose multiple reducers that act on separate parts of the state, i.e. properties of the state object.
The combineReducers helper function turns an object whose values are different reducing functions into a single reducing function you can pass to createStore .
You don't have to use combineReducers()
.
From the official Redux docs:
This helper is just a convenience! You can write your own
combineReducers
that works differently, or even assemble the state object from the child reducers manually and write a root reducing function explicitly, like you would write any other function.You may call
combineReducers
at any level of the reducer hierarchy. It doesn’t have to happen at the top. In fact you may use it again to split the child reducers that get too complicated into independent grandchildren, and so on.
So your proposed solution is definitely one way you can do it.
Thanks to @MichelleTilley for spearheading the train of thought!
I use Thunk to getState()
, prepare the data from the full store, then dispatch an action.
You can put the decision logic in the reducer or in the action. It is up to you. I prefer fat actions and thin reducers but there is no right/wrong.
Leonardo
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