Stop Writing Redux Action Constants by Hand with createSlice
Use Redux Toolkit's createSlice to generate action creators and immutable reducers without manual switch statements.
29 Jan 2026, 04:36 UTC

The manual Redux tax
Before Redux Toolkit, adding a feature to Redux usually meant writing three things by hand: an action type constant, an action creator function, and a reducer switch statement. You also had to copy state immutably—spreading objects and arrays at every level—because mutating the existing state object breaks React Redux’s change detection and can make the UI show stale values.
The result is boilerplate that grows with every new field, and it is easy to forget a spread operator or accidentally mutate a nested array.
What createSlice generates
Redux Toolkit’s createSlice accepts a slice name, an initial state, and a map of reducer functions. It generates action creators and action types automatically, using a sliceName/reducerName naming pattern. Inside each reducer, Redux Toolkit wraps your code with Immer, so you can assign to a draft state as if it were mutable; the library then produces the next immutable state.
Immer is a library that tracks changes to a temporary “draft” object and finalizes them into an immutable update. The protection only applies inside the reducer body; code outside the reducer must still treat state as read-only.
Worked example: a cart slice
This example defines a cart slice with actions to add and remove items. It assumes Redux Toolkit 2.x and a JavaScript project with a bundler.
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
items: [],
total: 0,
};
const cartSlice = createSlice({
name: 'cart',
initialState,
reducers: {
addItem(state, action) {
state.items.push(action.payload);
state.total += action.payload.price;
},
removeItem(state, action) {
const index = state.items.findIndex((item) => item.id === action.payload.id);
if (index !== -1) {
state.items.splice(index, 1);
state.total -= action.payload.price;
}
},
},
});
export const { addItem, removeItem } = cartSlice.actions;
export default cartSlice.reducer;
The addItem reducer pushes to state.items directly. That looks like a mutation, but Immer intercepts it inside the reducer and returns a new state object with the updated array. Outside this file, components dispatch addItem({ id: 1, price: 10 }) without importing any action constants.
Where to run and what to check
- Run
npm install @reduxjs/toolkitin your terminal at the project root. No special permissions are required. - Expected check: the package appears in
package.jsonandcreateSliceimports without a module error. - Risk: installing a new dependency can change your lockfile; review it before committing in a shared branch.
Trade-offs and limits
- Immer drafts only protect mutations inside the reducer body. Returning a mutated draft from an asynchronous callback or mutating state in a component still violates Redux immutability.
- Reducers must stay synchronous and pure. Async side effects belong in
createAsyncThunkor middleware, not inside a slice reducer. - Generated action types use
sliceName/reducerName, so two slices with the same name will collide. Keep slice names unique across the application.
Verify before you ship
- Open Redux DevTools, dispatch
addItem, and confirm the state tree shows a new object reference rather than the previous state mutated in place. - Write a unit test that calls
cartSlice.reducer(undefined, addItem({ id: 1, price: 10 }))and asserts the returned state is not the same reference asinitialState. - Add a middleware that logs
action.typeto confirm generated names follow thecart/addItemconvention.
Bottom line
createSlice removes the manual action constants and switch statements that make Redux verbose, while keeping the immutable update model that makes Redux predictable. Use it for synchronous state changes, keep async logic in thunks, and verify immutability with Redux DevTools or reference checks in tests.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.