I've got a Redux app with a fairly flat state tree. All sub reducers handle one separate part of the state, and they are tied together with combineReducers.

My problem is that in a few cases I need to access state part A in the sub reducer for state part B. The Redux FAQ has a section on this, with three suggestions:

  1. Reorganize the state tree.
  2. Don't use combineReducers.
  3. Use thunks to dispatch actions with the relevant parts of the state in them.

For me, #1 is not an option since my tree is already highly normalized. I feel that #2 would make my code unnececarily complex just to handle an edge case. So currently I am going with #3, but I don't really like it. To me, it makes more sense to keep the logic describing how the new state depends on the old state in the reducers, and not in the action creators.

This has led me to consider a fourth option, namely writing my own combineReducersWithFullState that gives the sub reducers the following signature:

(localState, action, fullState) => newLocalState

Is this a bad idea? Am I institutionalizing an anti-pattern here?

  • Can't you dispatch events and essentially listen for them in other components? That's usually how UI programming handles these sorts of issues. State Part A does something of interest to State Part B, so A dispatches an event that B subscribes to. – Greg Burghardt Aug 16 '18 at 11:41
  • @GregBurghardt Do you mean dispatch action? And where should I dispatch it? Doing it in the reducer is not allowed. Doing it in the actionCreator is what I am trying to get away from. – Anders Aug 16 '18 at 13:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.