Search⌘ K

Why Do We Need to Avoid Mutations?

Understand why reducers in Redux should never mutate the existing state to enable efficient change detection. Explore how reference comparison helps track changes in large state trees, allowing effective memory reuse and advanced features like undo, redo, and time travel debugging.

We'll cover the following...

The most important thing about reducers in Redux is that they should never mutate the existing state. There are several functions in JavaScript that can help when working with immutable objects. Before we look at those, however, let’s consider why this is so important.

Reason

One of the reasons behind the immutability requirement for reducers is due to change detection. After the store passes the current state and action to the root reducer, it and various UI components of the application need a way to determine what changes, if any, have happened to the global state. For small objects, a deep comparison or other similar methods might ...