How to get from one reducer to another?

I have a state { cities: [], activeCity: {} }

I need if in cities and added the city and it is only one - then make it active, ie add to activeCity.

As it is the right thing to do in conjunction react-redux?
August 19th 19 at 23:06
1 answer
August 19th 19 at 23:08
Place this state in one reducer. Then you will be able to see how many cities left in the array to change active city
It is a variant "in a forehead") is Accepted, thanks, but are there any other ways? - casey25 commented on August 19th 19 at 23:11
: you can make a nested reducer (one reducer causing the other state to be attached). Or action-Creator to put this logic... - orrin_Bergna commented on August 19th 19 at 23:14
: How can reducer cause reducer? Nada to dispatchit something - casey25 commented on August 19th 19 at 23:17
: not necessarily. You can do something like:
const innerReducer = (state = {}, action) => ...
const outerReducer = (state = {}, action) => {
 switch(action.type) {
 case: 'SOME_ACTION':
 return { ...state, someField: innerReducer(state.someField, action) }
 case 'OTHER_ACTION':
 return { ...state, someField: innerReducer(state.someField, { type: 'NEW_SHINY_ACTION', payload: 'some' }) }
}
}


You can call another reduser passing in the action. And you can even pass another action, but you must understand that this is not the same as the dispatch: this action go straight into reducer and should be simple objects with a type field. This causes the concept of reducer is still a pure function, without side-effects.
- orrin_Bergna commented on August 19th 19 at 23:20
: it is clear, interestingly, is architectural in General the right thing to do? - casey25 commented on August 19th 19 at 23:23
: why not? By adhering to these rules (the clean function) - I don't see the problem. This external reducer must match the signature of (state, action) => nextState. And for the domestic reduceron you can use any API like this: (state, something, otherParam) => nextState . - orrin_Bergna commented on August 19th 19 at 23:26

Find more questions by tags ReactRedux