Redux & Redux Toolkit Course
Redux & Redux Toolkit
/
Beginner

Mutable Syntax with Immer

Definition

The specific JavaScript methods (like `push`, `pop`, `splice`, or direct reassignment) that were historically banned in Redux, but are now safely allowed inside RTK thanks to Immer.

Explain Like I'm New

For years, Redux developers were terrified of using `.push()`. With RTK, `.push()` is your best friend. Immer intercepts these 'dangerous' methods and makes them safe.

Real World Example

Deleting an item from an array. In legacy Redux, you had to use `.filter()`. In RTK, you can just use `.splice()`, which is much easier to read.

Common Use Cases

  • Writing cleaner, faster reducers

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Is it safe to use `state.array.push()` inside a `createSlice` reducer?

intermediate

  • If you mutate the state in RTK, should you ALSO `return` the state?

Flash Cards

Question

Safe to push?

Click to reveal answer
Answer

YES! That is the entire point of Redux Toolkit.

Question

Mutate AND return?

Click to reveal answer
Answer

NO. In Immer, you can EITHER mutate the state, OR return a brand new state. You cannot do both. If you `state.push()`, you do not return anything.