Lesson 6 of 16
Understanding createSlice Simplifying Reducers and Actions
Introduces Redux Toolkit createSlice as the core abstraction for co-locating state, reducers, and action creators, showing how it eliminates boilerplate, enforces best practices, and preserves Redux’s predictable data flow through safe immutable updates.
Context / Motivation
In classic Redux, defining a new piece of state requires writing three separate things:
- An action type (string constant)
- An action creator (function returning
{ type, payload }) - A reducer (function handling that action via a switch statement)
For small apps, this was fine. But in real-world projects with dozens of features, it quickly became repetitive and error-prone. Every feature required three new files — constants, actions, reducers — all manually wired together.
Redux Toolkit’s createSlice() fixes this.
It unifies these three layers into a single conceptual unit called a slice:
a slice of your global state tree, complete with its logic and actions.
Conceptual Explanation
1. What is a Slice?
A slice represents one “domain” or “feature” of your application state. For example:
counterSlice→ state for countersuserSlice→ state for authentication and profilecartSlice→ state for the shopping cart
Each slice contains:
- The initial state
- A set of reducers that describe how state changes
- Automatically generated action creators for those reducers
The createSlice() function combines all of this into a single, cohesive module.
2. Anatomy of createSlice()
Here’s what a simple slice looks like:
import { createSlice } from "@reduxjs/toolkit";
const counterSlice = createSlice({
name: "counter", // unique slice name
initialState: { value: 0 },
reducers: {
increment: (state) => {
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});This one function call does the job of multiple files in vanilla Redux.
Let’s break it down:
| Field | Purpose |
|---|---|
name | Prefix used for action types (e.g. counter/increment) |
initialState | Default state value for this slice |
reducers | Object mapping case names to reducer functions |
| each reducer | Receives (state, action) and directly modifies state using Immer |
| returned object | Contains .reducer and .actions |
3. Automatic Action Generation
Redux Toolkit automatically generates action creators for each reducer key.
From the example above, RTK generates:
counterSlice.actions.increment(); // { type: 'counter/increment' }
counterSlice.actions.decrement(); // { type: 'counter/decrement' }
counterSlice.actions.incrementByAmount(5); // { type: 'counter/incrementByAmount', payload: 5 }You don’t write any of that manually.
It also automatically creates the action types:
counter/increment
counter/decrement
counter/incrementByAmount
This ensures all action type names are namespaced and consistent — no more typos or mismatched constants.
4. Using the Slice in Your Application
To make use of the slice, export both the reducer and the actions:
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
export default counterSlice.reducer;Then integrate it into your store:
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";
const store = configureStore({
reducer: {
counter: counterReducer,
},
});Finally, use it in your React components:
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "./counterSlice";
function Counter() {
const count = useSelector((state) => state.counter.value);
const dispatch = useDispatch();
return (
<div>
<h1>{count}</h1>
<button onClick={() => dispatch(increment())}>+</button>
<button onClick={() => dispatch(decrement())}>−</button>
</div>
);
}5. Mutating State with Immer
At first glance, you might wonder — isn’t state.value += 1 mutating the state?
Redux’s golden rule says reducers must be pure and immutable.
Under the hood, RTK uses a library called Immer, which provides a proxy wrapper around state. When you “mutate” it, Immer:
- Records all changes made to the draft object.
- Produces a new immutable state object with those changes applied.
So even though you write:
state.value += 1;RTK transforms it into an immutable update equivalent to:
return { ...state, value: state.value + 1 };This gives you the simplicity of mutation with the safety of immutability.
Real-World Illustration
Imagine building a shopping cart slice:
const cartSlice = createSlice({
name: "cart",
initialState: { items: [] },
reducers: {
addItem: (state, action) => {
state.items.push(action.payload);
},
removeItem: (state, action) => {
state.items = state.items.filter((i) => i.id !== action.payload.id);
},
clearCart: (state) => {
state.items = [];
},
},
});With that one block, you get:
- Automatic actions:
cart/addItem,cart/removeItem,cart/clearCart - A pure reducer that’s easy to read
- Safe immutability via Immer
- A namespaced state managed independently from other features
This replaces ~60 lines of classic Redux boilerplate with 15 lines of expressive code.
Evolution / Refinement
In classic Redux:
- Reducers were separated from actions.
- Type constants were manually synchronized.
- Each update required immutable copying.
Redux Toolkit’s createSlice():
- Co-locates logic and state by feature (more modular)
- Eliminates constants and action boilerplate
- Handles immutability automatically
- Generates action creators and action types for you
The pattern scales naturally. Each feature of your app (auth, products, settings, notifications) can live in its own slice, then be combined in one store — promoting feature encapsulation and mental clarity.
Principle / Takeaway
createSlice() turns Redux from a ceremony into a conversation.
Instead of orchestrating multiple files, you describe how your state should evolve in one place.
It achieves three goals simultaneously:
- Less code, fewer errors.
- More clarity, as actions and reducers are defined together.
- Same architecture, preserving Redux’s predictability and traceability.
It’s the heart of Redux Toolkit — the reason most teams finally fell back in love with Redux.
✅ Next Lesson Preview – Lesson 7:
Async Logic and Data Fetching with createAsyncThunk()
We’ll now handle asynchronous behavior — API requests, loading states, and error handling — using RTK’s built-in async mechanism. You’ll see how it replaces the old redux-thunk pattern with a cleaner, declarative approach.