Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide the updated parent state to each reducer #1447

Closed
wants to merge 1 commit into from

Conversation

chriddyp
Copy link

hi folks!

In my app I've found that some reducers occasionally need access to other parts of the state in order to compute their own state.

My fork just passes in the parent state for each reducer to (optionally) use.

This probably isn't the best way to do this as combineReducers aims to keep each slice of the store logically independent. I'm curious to hear how other folks have solved this.

@d6u
Copy link

d6u commented Feb 25, 2016

I found it most of the time I can solve this by making derived values computed when connecting with components by using https://github.com/reactjs/reselect. For two reason:

  • Store structure can be minimal this way, no dependencies inside store
  • Storing dependencies inside store can be confusing. Imagine this scenario: you make two API calls to the server, say tags, and posts, posts has tag_id on it. So in order to display the tag name on post you need to link posts with tags. If you calculate that in reducer, you could ended up having to handle the case when one arrived but the other didn't twice, one inside posts reducer, one in tags reducer. This duplication in large app is hard to manage.

@chriddyp
Copy link
Author

Good suggestion @d6u !

I've solved this for my particular use case by constructing an additional "fullState" reducer outside of combine reducers:

slicedReducers = combineReducers({stateSlice1, stateSlice2, stateSlice3});

module.exports = function(fullState, action) {
    let nextState = slicedReducers(fullState, action);
    return fullStateReducer(nextState, action);
}

@chriddyp chriddyp closed this Feb 25, 2016
@chriddyp chriddyp deleted the patch-1 branch February 25, 2016 20:18
@gaearon
Copy link
Contributor

gaearon commented Feb 25, 2016

Yep, both approaches work and I’m not sure whether we want to be opinionated here.
See also discussion in #1315.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants