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.


createSlice accepts a single configuration object parameter, with the following options:

function createSlice({
// A name, used in action types
name: string,
// The initial state for the reducer
initialState: any,
// An object of "case reducers". Key names will be used to generate actions.
reducers: Object<string, ReducerFunction | ReducerAndPrepareObject>
// An additional object of "case reducers", where the keys should be other
// action types, or a "builder callback" function used to add more reducers
| Object<string, ReducerFunction>
| ((builder: ActionReducerMapBuilder<State>) => void)


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.

const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: state => state + 1
// Will handle the action type `'counter/increment'`

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:

const todosSlice = createSlice({
name: 'todos',
initialState: [],
reducers: {
addTodo: {
reducer: (state, action) => {
prepare: text => {
const id = nanoid()
return { payload: { id, text } }


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.

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.

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.

Action creators that were generated using createAction may be used directly as the keys here, using computed property syntax.

const incrementBy = createAction('incrementBy')
name: 'counter',
initialState: 0,
reducers: {},
extraReducers: {
[incrementBy]: (state, action) => {
return state + action.payload
'some/other/action': (state, action) => {}

Note: If you are using TypeScript, we recommend using the builder callback API that is shown below. If you do not use the builder callback and are using TypeScript, you will need to use actionCreator.type or actionCreator.toString() to force the TS compiler to accept the computed property. Please see Usage With TypeScript for further details.

The "builder callback" API for extraReducers

Instead of using an object as extraReducers, you can also 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.

const incrementBy = createAction<number>('incrementBy')
const decrement = createAction('decrement')
name: 'counter',
initialState: 0,
reducers: {},
extraReducers: builder =>
.addCase(incrementBy, (state, action) => {
// action is inferred correctly here if using TS
// You can chain calls, or have separate `builder.addCase()` lines each time
.addCase(decrement, (state, action) => {})
// You can match a range of action types
action => action.type.endsWith('rejected'),
(state, action) => {}
// and provide a default case if no other handlers matched
.addDefaultCase((state, action) => {})

We recommend using this API if stricter type safety is necessary when defining reducer argument objects, 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

Return Value

createSlice will return an object that looks like:

name : string,
reducer : ReducerFunction,
actions : Record<string, ActionCreator>,
caseReducers: Record<string, CaseReducer>

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.


import { createSlice, createAction, PayloadAction } from '@reduxjs/toolkit'
import { createStore, combineReducers } from 'redux'
const incrementBy = createAction<number>('incrementBy')
const decrementBy = createAction<number>('decrementBy')
const counter = createSlice({
name: 'counter',
initialState: 0 as number,
reducers: {
increment: state => state + 1,
decrement: state => state - 1,
multiply: {
reducer: (state, action: PayloadAction<number>) => state * action.payload,
prepare: (value: number) => ({ payload: value || 2 }) // fallback if the payload is a falsy value
// "builder callback API", recommended for TypeScript users
extraReducers: builder => {
builder.addCase(incrementBy, (state, action) => {
return state + action.payload
builder.addCase(decrementBy, (state, action) => {
return state - action.payload
const user = createSlice({
name: 'user',
initialState: { name: '', age: 20 },
reducers: {
setUserName: (state, action) => { = action.payload // mutate the state all you want with immer
// "map object API"
extraReducers: {
[counter.actions.increment]: (state, action) => {
state.age += 1
const reducer = combineReducers({
counter: counter.reducer,
user: user.reducer
const store = createStore(reducer)
// -> { counter: 1, user: {name : '', age: 21} }
// -> { counter: 2, user: {name: '', age: 22} }
// -> { counter: 6, user: {name: '', age: 22} }
// -> { counter: 12, user: {name: '', age: 22} }
// -> "counter/decrement"
// -> { counter: 6, user: { name: 'eric', age: 22} }