Stop Writing Redux Action Types by Hand with createSlice
Reduce Redux boilerplate by co-locating reducers, initial state, and action creators with Redux Toolkit's createSlice.
21 May 2026, 06:12 UTC

A vanilla Redux file tax
In a vanilla Redux codebase, a single feature often touches three files: one for action type constants, one for action creators, and one for the reducer. Adding a field to state means editing the reducer, importing a new constant, and remembering to update every dispatch site. The immutable update logic is usually written with nested spread operators, and it is easy to accidentally mutate state directly, which breaks Redux's expectation that reducers return new state objects.
The useful takeaway is that Redux Toolkit's createSlice lets you define initial state, reducers, and generated action creators in one module. It uses Immer, so reducer code can look mutating while still producing immutable updates.
What createSlice actually replaces
createSlice accepts a slice name, initial state, and a reducers object. It returns an object containing auto-generated action creators and a reducer function. Action types follow the sliceName/actionName pattern, so you do not maintain separate constant files.
Before: split files
// cartActions.js
export const ADD_ITEM = 'cart/addItem';
export function addItem(product) {
return { type: ADD_ITEM, payload: product };
}
// cartReducer.js
import { ADD_ITEM } from './cartActions';
const initialState = { items: [] };
export function cartReducer(state = initialState, action) {
switch (action.type) {
case ADD_ITEM:
return { ...state, items: [...state.items, action.payload] };
default:
return state;
}
}
After: one slice
import { createSlice } from '@reduxjs/toolkit';
const cartSlice = createSlice({
name: 'cart',
initialState: { items: [] },
reducers: {
addItem(state, action) {
state.items.push(action.payload);
},
},
});
export const { addItem } = cartSlice.actions;
export default cartSlice.reducer;
Inside addItem, state.items.push looks like a direct mutation. Immer intercepts that draft, records the change, and returns a new state object. The original state reference is not altered.
Worked example: configure the store and verify
Run this in your project root terminal. No special permissions are required beyond normal access to install dependencies.
npm install @reduxjs/toolkit react-redux
Risk: if your project already pins an older Redux version, review peer dependency compatibility before installing.
import { configureStore } from '@reduxjs/toolkit';
import cartReducer, { addItem } from './cartSlice';
export const store = configureStore({
reducer: { cart: cartReducer },
});
store.dispatch(addItem({ id: 1, name: 'Notebook' }));
console.log(store.getState().cart.items);
Expected check: the console prints an array containing the new item. If you kept a reference to the pre-dispatch state, that object should remain unchanged. In Redux DevTools, the action type appears as cart/addItem, matching the generated naming pattern.
Limitations and where Immer protection ends
Immer protection applies only inside slice reducers. Mutating state in a component, a selector, or outside a reducer still violates Redux immutability rules. createSlice also does not handle side effects or data fetching; async logic still requires thunks or RTK Query.
Practical migration
- Install
@reduxjs/toolkitand confirmcreateSliceis exported. - Move one feature's reducer, initial state, and action creators into a slice.
- Write a test that dispatches a generated action and asserts the new state changed while the original state object stayed the same.
- Open Redux DevTools and verify action names match
sliceName/actionName.
Migrate one slice at a time. Old action type constants can remain until every dispatch site is updated.
Closing
createSlice is the official Redux Toolkit recommendation for typical synchronous state updates. It reduces file churn and makes reducers easier to read, but it is not a replacement for async logic or for disciplined state handling outside reducers.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.