Answer the question
In order to leave comments, you need to log in
How to communicate between individual reducers in redux?
Hello. Recently started working with redux. Before that, I wrote a couple of applications in Flux.
When working with flux stores, you can change several fields of the state tree during an action, that is, make one dependent on the other.
In redux, I ran into a problem: how to implement a dependency between two reducers.
For example, there is a form with two fields and a submit button, if the fields are empty the button is not available.
In flux called action CHANGE_FIELD and changed _field1Value or _field2Value to it, then called the checkValid(_field1Value,_field2Value) function and returned isValid
got a tree:
{
field1Value:'some text',
field2Value:'',
isValid:'false'
}
In redux, I found two ways out either to use one global reducer, or send field values to the button through mapStateToProps and form the isValid property for the button there.
But, still I would like to know if it is possible to implement the reducers dependency in redux?
Answer the question
In order to leave comments, you need to log in
There are usually no dependencies. But you can make nested reducers :
PS for forms use redux-form.
UPD1 In this case, you can manually call nested reducers:
function combinedReducer(state = initialState, action = {}) {
switch(action.type) {
case SOME_COMPLEX_ACTION:
return {
...state,
someProp: someOtherReducer(state.someProp, { type: OTHER_ACTION, payload: action.payload.something })
}
default:
return {
...state,
someProp: someOtherReducer(state.someProp)
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question