Skip to content

Commit b454c54

Browse files
committed
Add no mutation after jsx
1 parent 77c42df commit b454c54

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/content/reference/rules/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ The Rules of React are proven rules used at companies like Meta that help you ma
3232
* [Never pass around Hooks as regular values](/reference/rules/never-pass-around-hooks-as-regular-values): Hooks should only be called inside of components. Never pass it around as a regular value.
3333
* [Only call Hooks at the top level](/reference/rules/only-call-hooks-at-the-top-level): Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns.
3434
* [Only call Hooks from React functions](/reference/rules/only-call-hooks-from-react-functions): Don’t call Hooks from regular JavaScript functions.
35-
* [Values are immutable after being passed to JSX](/reference/rules/values-are-immutable-after-being-passed-to-jsx): TODO
35+
* [Values are immutable after being passed to JSX](/reference/rules/values-are-immutable-after-being-passed-to-jsx): Don't mutate values after they've been used in JSX. Move the mutation before the JSX is created.
3636
* [Return values and arguments to Hooks are immutable](/reference/rules/return-values-and-arguments-to-hooks-are-immutable): TODO

src/content/reference/rules/only-call-hooks-from-react-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Don’t call Hooks from regular JavaScript functions.
1111
Don’t call Hooks from regular JavaScript functions. Instead, you can:
1212

1313
✅ Call Hooks from React function components.
14-
✅ Call Hooks from custom Hooks (we’ll learn about them on the next page).
14+
✅ Call Hooks from [custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component).
1515

1616
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
1717

src/content/reference/rules/values-are-immutable-after-being-passed-to-jsx.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,41 @@ title: Values are immutable after being passed to JSX
33
---
44

55
<Intro>
6-
TODO
7-
</Intro>
6+
Don't mutate values after they've been used in JSX. Move the mutation before the JSX is created.
7+
</Intro>
8+
9+
---
10+
11+
When you use JSX in an expression, React may eagerly evaluate the JSX before the component finishes rendering. This means that mutating values after they've been passed to JSX can lead to outdated UIs, as React won't know to update the component's output.
12+
13+
```js {4}
14+
function Page({ colour }) {
15+
const styles = { colour, size: "large" };
16+
const header = <Header styles={styles} />;
17+
styles.size = "small"; // ❌ styles was already used in the JSX above!
18+
const footer = <Footer styles={styles} />;
19+
return (
20+
<>
21+
{header}
22+
<Content />
23+
{footer}
24+
</>
25+
);
26+
}
27+
```
28+
29+
```js {4}
30+
function Page({ colour }) {
31+
const headerStyles = { colour, size: "large" };
32+
const header = <Header styles={headerStyles} />;
33+
const footerStyles = { colour, size: "small" }; // ✅ we created a new value
34+
const footer = <Footer styles={footerStyles} />;
35+
return (
36+
<>
37+
{header}
38+
<Content />
39+
{footer}
40+
</>
41+
);
42+
}
43+
```

0 commit comments

Comments
 (0)