Skip to content

Commit 30ba707

Browse files
PUNITHKUMAR RPUNITHKUMAR R
authored andcommitted
Docs: clarify state mutation and add example
Explain why directly mutating objects in state is bad and add a short code example showing immutable updates. Fixes readability for beginners.
1 parent 2534424 commit 30ba707

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/content/learn/state-as-a-snapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ label, textarea { margin-bottom: 10px; display: block; }
305305

306306
</Sandpack>
307307

308-
**React keeps the state values "fixed" within one render's event handlers.** You don't need to worry whether the state has changed while the code is running.
308+
**React keeps the state value “fixed within one render, meaning it won’t update until the next render cycle.** You don't need to worry whether the state has changed while the code is running.
309309

310310
But what if you wanted to read the latest state before a re-render? You'll want to use a [state updater function](/learn/queueing-a-series-of-state-updates), covered on the next page!
311311

src/content/learn/updating-objects-in-state.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Updating Objects in State
44

55
<Intro>
66

7-
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly. Instead, when you want to update an object, you need to create a new one (or make a copy of an existing one), and then set the state to use that copy.
7+
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly.This is because React does not detect direct mutations, so the UI will not update. Instead, when you want to update an object, you need to create a new one (or make a copy of an existing one), and then set the state to use that copy.
88

99
</Intro>
1010

@@ -45,7 +45,8 @@ Technically, it is possible to change the contents of _the object itself_. **Thi
4545
position.x = 5;
4646
```
4747

48-
However, although objects in React state are technically mutable, you should treat them **as if** they were immutable--like numbers, booleans, and strings. Instead of mutating them, you should always replace them.
48+
However, although objects in React state are technically mutable, you should treat them **as if** they were immutable--like numbers, booleans, and strings.This helps React correctly detect changes and update the UI as expected.
49+
Instead of mutating them, you should always replace them.
4950

5051
## Treat state as read-only {/*treat-state-as-read-only*/}
5152

0 commit comments

Comments
 (0)