State with Objects and Arrays
When state holds objects or arrays, you need to be careful to update them correctly. React requires immutable updates to detect changes.
The Immutability Rule
React uses reference comparison to detect changes. You must create new objects/arrays, not mutate existing ones:
Updating Objects
Always spread the existing object and override specific properties:
Updating Arrays
Arrays also need immutable updates using methods that return new arrays:
Common Patterns
Exercise: Update Nested Object
Exercise: Array CRUD Operations
Exercise: Complex State Update
Key Takeaways
- Never mutate - Always create new objects/arrays
- Spread operator -
{...obj}and[...arr]create copies - Nested updates - Spread at each level of nesting
- Array methods - Use map, filter, spread for updates
- Reference check - React needs new references to re-render
Up next · Lesson 11 of 33
Common useState Patterns
9 min read
10 of 33 lessons done

Ask Byte about this lesson
A private chat about this lesson. Byte has read the whole lesson, answers in seconds, and shows the section each answer comes from. Ask follow-ups in any language.
Questions & Answers
Public questions about this lesson. The AI Tutor answers right away in your language, the FreeAcademy team checks answers, and every learner can read them.

