ReactIntermediate#state

Why should you not mutate state directly?

React compares references to decide what changed, so mutating an object or array in place can skip the re-render entirely and makes bugs hard to trace. Always produce a new value.

Example
// wrong
items.push(x); setItems(items);
// right
setItems([...items, x]);
setUser(u => ({ ...u, name: 'New' }));

Related Questions

1
ReactIntermediate#routing

How do you handle routing in React?

Open
2
ReactBeginner#props

What are default props and prop validation today?

Open
3
ReactBeginner#fundamentals

What is the key difference between class and function components?

Open