M
M
Maxim Nikulin2016-03-16 22:28:58
JavaScript
Maxim Nikulin, 2016-03-16 22:28:58

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

2 answer(s)
N
Nikita Gushchin, 2016-03-17
@max_nikulin

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)
      }
  }
}

S
Superilya, 2016-12-17
@Superilya

redux of the brain, it is not needed for this, so the associated reducers are a crutch anyway. You need related reducers, connect the button to the data and disable it depending on them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question