Home / Companies / SuperTokens / Blog / Post Details
Content Deep Dive

Why is redux state immutable?

Blog post from SuperTokens

Post Details
Company
Date Published
Author
Rishabh Poddar
Word Count
872
Language
English
Hacker News Points
-
Summary

Redux requires the state to be immutable to efficiently detect changes and trigger re-renders in React applications. This immutability is achieved by creating a new copy of the state whenever an update is needed, rather than directly altering the existing state. The distinction between "value" and "reference" is crucial, as React components decide to re-render based on whether the references of state and props have changed. A deep comparison of all fields within a potentially complex state structure can be inefficient, so a shallow reference check is preferred. By ensuring that new state instances have different references, Redux optimizes performance without missing updates, as any change in reference is assumed to indicate a change in value, thereby triggering a re-render only when necessary.