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.
createSlice accepts a single configuration object parameter, with the following options:
The initial state value for this slice of state.
A string name for this slice of state. Generated action type constants will use this as a prefix.
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.
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:
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:
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.
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
reducers, these case reducers will also be passed to
createReducer and may "mutate" their state safely.
If two fields from
extraReducers happen to end up with the same action type string,
the function from
reducers will be used to handle that action type.
extraReducers "builder callback" notation
The recommended way of using
extraReducers is to use a callback that receives a
This builder notation is also the only way to add matcher reducers and default case reducers to your slice.
It's particularly useful for working with actions produced by
See the "Builder Callback Notation" section of the
createReducer reference for details on how to use
extraReducers "map object" notation
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 the
builder callbackand are using TypeScript, you will need to use
actionCreator.toString()to force the TS compiler to accept the computed property. Please see Usage With TypeScript for further details.
createSlice will return an object that looks like:
Each function defined in the
reducers argument will have a corresponding action creator generated using
and included in the result's
actions field using the same function name.
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.