createSlice#
A function that accepts an initial state, an object full of reducer functions, and a "slice name", and automatically generates action creators and action types that correspond to the reducers and state.
This API is the standard approach for writing Redux logic.
Internally, it uses createAction and createReducer, so
you may also use Immer to write "mutating" immutable updates:
- TypeScript
- JavaScript
Parameters#
createSlice accepts a single configuration object parameter, with the following options:
initialState#
The initial state value for this slice of state.
name#
A string name for this slice of state. Generated action type constants will use this as a prefix.
reducers#
An object containing Redux "case reducer" functions (functions intended to handle a specific action type, equivalent to a single case statement in a switch).
The keys in the object will be used to generate string action type constants, and these will show up in the Redux DevTools Extension when they are dispatched. Also, if any other part of the application happens to dispatch an action with the exact same type string, the corresponding reducer will be run. Therefore, you should give the functions descriptive names.
This object will be passed to createReducer, so the reducers may safely "mutate" the
state they are given.
- TypeScript
- JavaScript
Customizing Generated Action Creators#
If you need to customize the creation of the payload value of an action creator by means of a prepare callback, the value of the appropriate field of the reducers argument object should be an object instead of a function. This object must contain two properties: reducer and prepare. The value of the reducer field should be the case reducer function while the value of the prepare field should be the prepare callback function:
- TypeScript
- JavaScript
extraReducers#
One of the key concepts of Redux is that each slice reducer "owns" its slice of state, and that many slice reducers
can independently respond to the same action type. extraReducers allows createSlice to respond to other action types
besides the types it has generated.
As case reducers specified with extraReducers are meant to reference "external" actions, they will not have actions generated in slice.actions.
As with reducers, these case reducers will also be passed to createReducer and may "mutate" their state safely.
If two fields from reducers and extraReducers happen to end up with the same action type string,
the function from reducers will be used to handle that action type.
The extraReducers "builder callback" notation#
The recommended way of using extraReducers is to use a callback that receives a ActionReducerMapBuilder instance.
This builder notation is also the only way to add matcher reducers and default case reducers to your slice.
- TypeScript
- JavaScript
We recommend using this API as it has better TypeScript support (and thus, IDE autocomplete even for JavaScript users), as it will correctly infer the action type in the reducer based on the provided action creator.
It's particularly useful for working with actions produced by createAction and createAsyncThunk.
See the "Builder Callback Notation" section of the createReducer reference for details on how to use builder.addCase, builder.addMatcher, and builder.addDefault
The extraReducers "map object" notation#
Like reducers, extraReducers can be an object containing Redux case reducer functions. However, the keys should
be other Redux string action type constants, and createSlice will not auto-generate action types or action creators
for reducers included in this parameter.
Action creators that were generated using createAction may be used directly as the keys here, using
computed property syntax.
Note: If you are using TypeScript, we recommend using the
builder callbackAPI that is shown above. If you do not use thebuilder callbackand are using TypeScript, you will need to useactionCreator.typeoractionCreator.toString()to force the TS compiler to accept the computed property. Please see Usage With TypeScript for further details.
Return Value#
createSlice will return an object that looks like:
Each function defined in the reducers argument will have a corresponding action creator generated using createAction
and included in the result's actions field using the same function name.
The generated reducer function is suitable for passing to the Redux combineReducers function as a "slice reducer".
You may want to consider destructuring the action creators and exporting them individually, for ease of searching for references in a larger codebase.
Note: the result object is conceptually similar to a "Redux duck" code structure. The actual code structure you use is up to you, but there are a couple caveats to keep in mind:
- Actions are not exclusively limited to a single slice. Any part of the reducer logic can (and should!) respond to any dispatched action.
- At the same time, circular references can cause import problems. If slices A and B are defined in separate files, and each file tries to import the other so it can listen to other actions, unexpected behavior may occur.
Examples#
- TypeScript
- JavaScript