Replacing, Not Editing
The rule. Never change an object or array that is in state. Make a new one.
Why. React decides whether to re-render by asking whether the new value is the same value as the old one. Editing an array in place leaves it the same array, so React sees nothing to do and the screen does not update, even though the data really did change.
Derived values are not state. If you can calculate something from state you already have, calculate it during the render. A second piece of state holding items.length can fall out of step with items. A plain const count = items.length cannot.
Mark this lesson complete
Signed in, your progress follows you to every device.