Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a reason to keep state completely immutable in React?

Take this example of state:

const [state, setState] = useState({ x: 1, nested: { y: 2, z: 3 } });

The immutable version of a handler to increment the value of y would be as follows:

function incrementY() {
    setState((state) => {
      return { ...state, nested: { ...state.nested, y: state.nested.y + 1 } };
    });
}

The next version creates a new state object, but mutates the nested object:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function incrementY() {
    setState((state) => {
      state.nested.y += 1;
      return { ...state };
    });
}

Both versions will lead to a re-render of the component. Now, let’s assume this component renders a child:

return (
    <div onClick={() => incrementY()}>
      <Child nested={state.nested} />
    </div>
);

If I understand correctly, a re-render of the parent component will always lead to a re-render of the child component (even when the reference to the nested object stays the same).

I’m aware that immutable state can be useful when there is a need to keep the history of states (e.g. for CTRL + Z functionality). But are there any reasons specific to React that I’m missing?

>Solution :

If I understand correctly, a re-render of the parent component will always lead to a re-render of the child component

By default yes, but the child component can use React.memo (or other techniques for class components) to skip rendering if its props have not changed. If you mutate the nested object, then pass that object to the child, it will look to the child like nothing has changed.

So if you want to be able to reliably use react’s tools for skipping rendering, then you must make your state immutable at all levels.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading