R
R
RokeAlvo2019-08-05 19:20:09
React
RokeAlvo, 2019-08-05 19:20:09

How to access store from reducer?

Let's say the store is composite:

rootReducer = combineReducers({
  reducer1: reducer1,
reducer2: reducer2
})

and we need, for some action that is processed in reducer1, to write to the store a value that depends on the payload of the action and the state of reducer2:
function reducer1(state ={data: []}, action) {
  switch (action.type) {
    case SOME_ACTION:
      return {...state, data[store.reducer2.id]: action.payload}
    
    default:
      return state
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-08-05
@RokeAlvo

You need to use middleware. For example redux-thunk

const action = value => (dispatch, getState) => {
  const otherValue = otherValueSelector(getState());
  
  const payload = calculate(value, otherValue);

  dispatch(otherAction(payload));
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question