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

Make dependent reducer explanation more prominent #1653

Closed
gaearon opened this issue Apr 21, 2016 · 5 comments
Closed

Make dependent reducer explanation more prominent #1653

gaearon opened this issue Apr 21, 2016 · 5 comments
Labels

Comments

@gaearon
Copy link
Contributor

gaearon commented Apr 21, 2016

This is a pattern we don’t encourage to use all the time, but sometimes it’s handy.
We should really include it in the FAQ, or better, write a recipe.

https://twitter.com/dan_abramov/status/723157303340326913

@gaearon gaearon added the docs label Apr 21, 2016
@gaearon
Copy link
Contributor Author

gaearon commented Apr 21, 2016

Also I still think this shouldn’t be the default behavior for combineReducers(). The whole point is you do this explicitly and structure data dependencies any way you like:

export default function todos(state = {}, action) {
  return {
    byId: byId(state.byId, action, state),
    listedIds: listedIds(state.listedIds, action, state)
  }
}

or

export default function todos(state = {}, action) {
  var nextById = byId(state.byId, action, state.listedIds);
  var nextListedIds = listedIds(state.listedIds, action, nextById);
  return {
    listedIds: nextListedIds,
    byId: nextById
  }
}

or

export default function todos(state = {}, action) {
  var nextListedIds = listedIds(state.listedIds, action, state.byId);
  var nextById = byId(state.byId, action, nextListedIds);
  return {
    listedIds: nextListedIds,
    byId: nextById
  }
}

The order is up to you, depending on your needs.

@markerikson
Copy link
Contributor

Writing a "Structuring Reducers" recipe page is still on my to-do list. Might have time to take a first crack at it this weekend. We'd then be able to add a link to it in the "sharing state" FAQ question.

@mattiamanzati
Copy link

mattiamanzati commented Apr 22, 2016

Some month ago I wrote a micro-module to perform this task and combine reducers specifying their dependencies, turned out that this is mostly a rare case. Turns out it's handy but using side-effects handling could achieve the same result.
https://github.com/KodersLab/topologically-combine-reducers

@gaearon
Copy link
Contributor Author

gaearon commented Apr 22, 2016

@mattiamanzati That’s a neat module 😄 .

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

No branches or pull requests

3 participants