# Redux Toolkit: createSlice & Immer

> Eliminate legacy Redux boilerplate and safely write mutating logic using the Immer library.

Subject: State Management | Lesson 11 of 22 | 7 min read
Interactive lesson: https://codewithanimation.in/state-management/rtk-createslice-and-immer
Previous: [The Redux Mental Model](https://codewithanimation.in/state-management/redux-mental-model.md) | Next: [Redux Toolkit: useDispatch & useSelector](https://codewithanimation.in/state-management/usedispatch-useselector.md)

## What is createSlice?

`createSlice` is the standard, modern API for writing Redux logic. It eliminates the traditional Redux boilerplate of manually writing action type constants, action creator functions, and giant switch statements.

You simply provide a name, an initial state, and an object of reducer functions. Redux Toolkit automatically generates the corresponding action creators and action types for you.

## The Immer Proxy Magic

In traditional Redux, you must never mutate the state. Updating nested objects required tedious and error-prone spread syntax (e.g., `...state, user: { ...state.user, age: 30 }`).

Redux Toolkit solves this by wrapping your state in an **Immer Proxy**. Inside `createSlice`, you can write code that *looks* like it is mutating the state directly (`state.value += 1`). Immer intercepts these mutations and safely translates them into a brand new immutable object behind the scenes via a process called structural sharing.

## Working with Draft State

The `state` argument passed to your case reducers is not the raw Redux state. It is a temporary **draft state** managed by Immer.

This draft mirrors the exact structure of your real state tree. You can push to arrays, reassign object properties, and delete keys. Once your reducer finishes executing, Immer processes the recorded mutations and creates the new state tree.

## Common mistake: The Golden Rule of Immer Reducers

There is one critical rule you must follow: **You can either mutate the draft state, OR return a new state object, but you cannot do both.**

If you try to mutate the state and return a value in the same reducer, Immer cannot determine which change to apply and will throw an error: `An immer producer returned a new value and modified its draft`.

## Tip: Beware the Implicit Arrow Return

A common trap is using an implicit return with an arrow function: `increment: (state) => state.value += 1`.

In JavaScript, `state.value += 1` evaluates to the new number. The arrow function implicitly returns that number, meaning you just broke the Golden Rule (you mutated the draft *and* returned a value). Always use curly braces for mutations: `increment: (state) => { state.value += 1; }`.

## Try it: Interactive Immer Lab

Use the top toolbar to dispatch actions. Watch how Redux Toolkit lets you write standard mutating JavaScript syntax while Immer seamlessly outputs a brand new immutable state object under the hood.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/rtk-createslice-and-immer

## Check yourself

### Question 1

Which of the following reducers will throw an Immer runtime error?

- A. (state, action) => { state.user.name = action.payload; }
- B. (state, action) => { return { ...state, user: action.payload }; }
- C. (state, action) => { state.count += 1; return { ...state }; }

Correct answer: C

Option C will throw an error because it violates the Golden Rule: it modifies the draft (`state.count += 1`) AND returns a new object. Immer requires you to choose one or the other.

- Why not A: This is correct. It mutates the draft and returns nothing.
- Why not B: This is correct. It returns a new object without mutating the draft.

## Remember this

- `createSlice` automatically generates action creators and action types based on the reducer names.
- Immer wraps your state in a Proxy, allowing you to safely write "mutating" code like `state.push()`.
- The state argument in a case reducer is an Immer draft, not the raw state.
- You must choose to either mutate the draft OR return a new value. Never do both.
