createReducer()
#
#
OverviewA utility that simplifies creating Redux reducer functions. It uses Immer internally to drastically simplify immutable update logic by writing "mutative" code in your reducers, and supports directly mapping specific action types to case reducer functions that will update the state when that action is dispatched.
Redux reducers are often implemented using a switch
statement, with one case
for every handled action type.
This approach works well, but is a bit boilerplate-y and error-prone. For instance, it is easy to forget the default
case or
setting the initial state.
The createReducer
helper streamlines the implementation of such reducers. It supports two different forms of defining case
reducers to handle actions: a "builder callback" notation and a "map object" notation. Both are equivalent, but the "builder callback"
notation is preferred.
With createReducer
, your reducers instead look like:
- TypeScript
- JavaScript
#
Usage with the "Builder Callback" NotationThis overload accepts a callback function that receives a builder
object as its argument.
That builder provides addCase
, addMatcher
and addDefaultCase
functions that may be
called to define what actions this reducer will handle.
The recommended way of using createReducer
is the builder callback notation, as it works best with TypeScript and most IDEs.
#
Parameters- initialState The initial state that should be used when the reducer is called the first time.
- builderCallback A callback that receives a builder object to define
case reducers via calls to
builder.addCase(actionCreatorOrType, reducer)
.
#
Example Usage- TypeScript
- JavaScript
#
Builder Methodsbuilder.addCase
#
Adds a case reducer to handle a single exact action type.
All calls to builder.addCase
must come before any calls to builder.addMatcher
or builder.addDefaultCase
.
#
Parameters- actionCreator Either a plain action type string, or an action creator generated by
createAction
that can be used to determine the action type. - reducer The actual case reducer function.
builder.addMatcher
#
Allows you to match your incoming actions against your own filter function instead of only the action.type
property.
If multiple matcher reducers match, all of them will be executed in the order
they were defined in - even if a case reducer already matched.
All calls to builder.addMatcher
must come after any calls to builder.addCase
and before any calls to builder.addDefaultCase
.
#
Parameters- matcher A matcher function. In TypeScript, this should be a type predicate function
- reducer The actual case reducer function.
- TypeScript
- JavaScript
builder.addDefaultCase
#
Adds a "default case" reducer that is executed if no case reducer and no matcher reducer was executed for this action.
#
Parameters- reducer The fallback "default case" reducer function.
- TypeScript
- JavaScript
#
Usage with the "Map Object" NotationThis overload accepts an object where the keys are string action types, and the values are case reducer functions to handle those action types.
While this notation is a bit shorter, it works only in JavaScript, not TypeScript and has less integration with IDEs, so we recommend the "builder callback" notation in most cases.
#
Parameters- initialState The initial state that should be used when the reducer is called the first time.
- actionsMap An object mapping from action types to case reducers, each of which handles one specific action type.
- actionMatchers An array of matcher definitions in the form
{matcher, reducer}
. All matching reducers will be executed in order, independently if a case reducer matched or not. - defaultCaseReducer A "default case" reducer that is executed if no case reducer and no matcher reducer was executed for this action.
#
Example UsageAction creators that were generated using createAction
may be used directly as the keys here, using computed property syntax:
#
Matchers and Default Cases as ArgumentsThe most readable approach to define matcher cases and default cases is by using the builder.addMatcher
and builder.addDefaultCase
methods described above, but it is also possible to use these with the object notation by passing an array of {matcher, reducer}
objects as the third argument, and a default case reducer as the fourth argument:
#
Direct State MutationRedux requires reducer functions to be pure and treat state values as immutable. While this is essential for making state updates predictable and observable, it can sometimes make the implementation of such updates awkward. Consider the following example:
- TypeScript
- JavaScript
The addTodo
reducer is straightforward if you know the ES6 spread syntax. However, the code for toggleTodo
is much less straightforward, especially considering that it only sets a single flag.
To make things easier, createReducer
uses immer to let you write reducers as if they were mutating the state directly. In reality, the reducer receives a proxy state that translates all mutations into equivalent copy operations.
- TypeScript
- JavaScript
Writing "mutating" reducers simplifies the code. It's shorter, there's less indirection, and it eliminates common mistakes made while spreading nested state. However, the use of Immer does add some "magic", and Immer has its own nuances in behavior. You should read through pitfalls mentioned in the immer docs . Most importantly, you need to ensure that you either mutate the state
argument or return a new state, but not both. For example, the following reducer would throw an exception if a toggleTodo
action is passed:
- TypeScript
- JavaScript
#
Multiple Case Reducer ExecutionOriginally, createReducer
always matched a given action type to a single case reducer, and only that one case reducer would execute for a given action.
Using action matchers changes that behavior, as multiple matchers may handle a single action.
For any dispatched action, the behavior is:
- If there is an exact match for the action type, the corresponding case reducer will execute first
- Any matchers that return
true
will execute in the order they were defined - If a default case reducer is provided, and no case or matcher reducers ran, the default case reducer will execute
- If no case or matcher reducers ran, the original existing state value will be returned unchanged
The executing reducers form a pipeline, and each of them will receive the output of the previous reducer:
- TypeScript
- JavaScript
#
Logging Draft State ValuesIt's very common for a developer to call console.log(state)
during the development process. However, browsers display Proxies in a format that is hard to read, which can make console logging of Immer-based state difficult.
When using either createSlice
or createReducer
, you may use the current
utility that we re-export from the immer
library. This utility creates a separate plain copy of the current Immer Draft
state value, which can then be logged for viewing as normal.
- TypeScript
- JavaScript