Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Removes the
logOut()
thunk action creator and handles all state transitions directly in reducers.Flow looks like this:
Workspace
component dispatchesUSER_LOGGED_OUT
projects
reducer consumesUSER_LOGGED_OUT
ui
reducer consumesUSER_LOGGED_OUT
user
reducer consumesUSER_LOGGED_OUT
We do introduce a new pattern to reducers, in order for the
project
reducer to be able to do the right thing.When the user logs out, we want to clear all projects out of the list except for the current project. This requires the projects reducer to know what the current project is.
There are several ways to solve this problem without giving the projects
reducer access to the rest of the store:
currentProject
subtree, also marking the object inprojects
ascurrent
However, each of these approaches has significant disadvantages:
USER_LOGGED_OUT
is an implementation detail of the store; the component that initially dispatches the action should not need to know thisContra the author of this highly upvoted GitHub issue comment, I don’t think it’s an antipattern for reducers to have access to the entire store. In fact, this is the great strength of Redux—we’re able to model all state transitions based on a universally-agreed-upon answer to the question “what is the current state of the world?” The
combineReducers
approach to isolating the reducer logic for different parts of the subtree is a very useful tool for organizing code; it’s not a mandate to only organize code that way.Further, options 1 and 2 above feel a bit ridiculous because, fundamentally, reducers do have access to the entire state. Why would we jump through hoops just to give the reducer information it already has access to?
So: establish a pattern that reducer modules may export a named
reduceRoot
function, which takes the entire state and performs reductions on it. The top-level root reducer will import this function and apply it to the state after running the isolated reducers using thereduce-reducers
module.