Reducers
Redux FAQ: Reducers
How do I share state between two reducers? Do I have to use combineReducers?
The suggested structure for a Redux store is to split the state object into multiple “slices” or “domains” by key, and provide a separate reducer function to manage each individual data slice. This is similar to how the standard Flux pattern has multiple independent stores, and Redux provides the combineReducers utility function to make this pattern easier. However, it's important to note that combineReducers is not required—it is simply a utility function for the common use case of having a single reducer function per state slice, with plain JavaScript objects for the data.
Many users later want to try to share data between two reducers, but find that combineReducers does not allow them to do so. There are several approaches that can be used:
- First, check whether you need to share state at all, or whether several slices just need to respond to the same action. The latter is the common case, and it needs no special handling: each
createSlicecan handle actions from other slices (or fromcreateAsyncThunk) in itsextraReducersfield. See Allow Many Reducers to Respond to the Same Action in the Style Guide. - If a reducer needs to know data from another slice of state, the state tree shape may need to be reorganized so that a single reducer is handling more of the data.
- You may need to write some custom functions for handling some of these actions. This may require replacing
combineReducerswith your own top-level reducer function. You can also use a utility such as reduce-reducers to runcombineReducersto handle most actions, but also run a more specialized reducer for specific actions that cross state slices. BeyondcombineReducersshows this approach. - Thunks and listener middleware effects have access to the entire state through
getState(). A thunk can retrieve additional data from the state and put it in the action it dispatches, so that each reducer has enough information to update its own state slice.
In general, remember that reducers are just functions—you can organize them and subdivide them any way you want, and you are encouraged to break them down into smaller, reusable functions (“reducer composition”). While you do so, you may pass a custom third argument from a parent reducer if a child reducer needs additional data to calculate its next state. You just need to make sure that together they follow the basic rules of reducers: (state, action) => newState, and update state immutably rather than mutating it directly.
Further information
Documentation
Discussions
- #601: A concern on combineReducers, when an action is related to multiple reducers
- #1400: Is passing top-level state object to branch reducer an anti-pattern?
- Stack Overflow: Accessing other parts of the state when using combined reducers?
- Stack Overflow: Reducing an entire subtree with redux combineReducers
- Sharing State Between Redux Reducers
Do I have to use the switch statement to handle actions?
No. You are welcome to use any approach you'd like to respond to an action in a reducer. The switch statement was the most common approach in hand-written reducers, but it's fine to use if statements, a lookup table of functions, or to create a function that abstracts this away. In fact, while Redux does require that action objects contain a type field, your reducer logic doesn't even have to rely on that to handle the action.
Today the standard approach is createSlice, which is a lookup table: each function in its reducers field handles one action type, and the slice generates the matching action creators and the combined reducer for you. The createReducer builder callback does the same for reducers that aren't part of a slice.
Further information
Documentation
- Style Guide: Use Redux Toolkit for Writing Redux Logic
- Using Redux: Reducing Boilerplate
- Using Redux: Structuring Reducers - Splitting Reducer Logic
Discussions