You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/rules/components-and-hooks-must-be-pure.md
+43-7
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Components and Hooks must be pure
3
3
---
4
4
5
5
<Intro>
6
-
TODO
6
+
[Purity](/learn/keeping-components-pure) makes your code easier to understand and debug.
7
7
</Intro>
8
8
9
9
<InlineToc />
@@ -76,7 +76,7 @@ function FriendList({ friends }) {
76
76
77
77
There is no need to contort your code to avoid local mutation. In particular, [`Array.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) could also be used here for brevity, but there is nothing wrong with creating a local array and then pushing items into it during render.
78
78
79
-
Even though it looks like we are mutating `items`, the key point to note is that this code only does so locally – the mutation isn't "remembered" when the component is rendered again. In other words, `items` only stays around as long as the component does. Because `items` is always recreated every time `<FriendList />` is rendered, the component will always returns the same result.
79
+
Even though it looks like we are mutating `items`, the key point to note is that this code only does so locally – the mutation isn't "remembered" when the component is rendered again. In other words, `items` only stays around as long as the component does. Because `items` is always recreated every time `<FriendList />` is rendered, the component will always return the same result.
80
80
81
81
On the other hand, if `items` was created outside of the component, it holds on to its previous values and remembers changes:
82
82
@@ -175,8 +175,7 @@ function Counter() {
175
175
176
176
## Return values and arguments to Hooks are immutable {/*return-values-and-arguments-to-hooks-are-immutable*/}
177
177
178
-
Once values are passed to a Hook, neither the calling code nor the Hook should
179
-
modify them. Like props in JSX, values become immutable when passed to a Hook.
178
+
Once values are passed to a Hook, you should not modify them. Like props in JSX, values become immutable when passed to a Hook.
180
179
181
180
```js {4}
182
181
functionuseIconStyle(icon) {
@@ -201,8 +200,7 @@ function useIconStyle(icon) {
201
200
}
202
201
```
203
202
204
-
The custom Hook might have used the hook arguments as dependencies to memoize
205
-
values inside it.
203
+
The custom Hook might have used the hook arguments as dependencies to memoize values inside it.
206
204
207
205
```js {4}
208
206
functionuseIconStyle(icon) {
@@ -232,4 +230,42 @@ icon = { ...icon, enabled: false }; // ✅ make a copy instead
232
230
style =useIconStyle(icon); // new value of `style` is calculated
233
231
```
234
232
235
-
Similarly, it's important to not modify the return values of hooks, as they have been memoized.
233
+
Similarly, it's important to not modify the return values of hooks, as they may have been memoized.
234
+
235
+
## Values are immutable after being passed to JSX {/*values-are-immutable-after-being-passed-to-jsx*/}
236
+
237
+
Don't mutate values after they've been used in JSX. Move the mutation before the JSX is created.
238
+
239
+
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.
240
+
241
+
```js {4}
242
+
functionPage({ colour }) {
243
+
conststyles= { colour, size:"large" };
244
+
constheader=<Header styles={styles} />;
245
+
styles.size="small"; // ❌ styles was already used in the JSX above!
246
+
constfooter=<Footer styles={styles} />;
247
+
return (
248
+
<>
249
+
{header}
250
+
<Content />
251
+
{footer}
252
+
</>
253
+
);
254
+
}
255
+
```
256
+
257
+
```js {4}
258
+
functionPage({ colour }) {
259
+
constheaderStyles= { colour, size:"large" };
260
+
constheader=<Header styles={headerStyles} />;
261
+
constfooterStyles= { colour, size:"small" }; // ✅ we created a new value
Copy file name to clipboardExpand all lines: src/content/reference/rules/index.md
+6-3
Original file line number
Diff line number
Diff line change
@@ -17,12 +17,15 @@ We strongly recommend using Strict Mode alongside React's ESLint plugin to help
17
17
18
18
You can think of React's constraints like the grammatical rules and patterns of languages: they constrain what we can do with words, so that we can correctly and efficiently communicate our thoughts.
19
19
20
-
These rules have been used in the design of all of React's features over the years. React's Strict Mode enforces several of these rules at runtime in DEV mode, and with the release of React's upcoming compiler, more rules will now be statically checked to help you find more bugs as well as allow for correct optimization of your code.
20
+
These rules have been used in the design of all of React's features over the years. Enabling Strict Mode catches bugs caused by breaking these rules, and with the release of React's upcoming compiler, more rules will now be statically checked to help you find more bugs as well as allow for correct optimization of your code.
21
+
22
+
<Note>
23
+
Strict Mode checks are run in development mode only; they do not impact the production build.
24
+
</Note>
21
25
22
26
The Rules of React are proven rules used at companies like Meta that help you maintain an application and codebase that scales with you. When followed, your codebase becomes easier to understand and maintain, is less buggy, and helps React ensure your code runs efficiently by default.
23
27
</DeepDive>
24
28
25
29
*[Components and Hooks must be pure](/reference/rules/components-and-hooks-must-be-pure)
26
30
*[React orchestrates Components and Hooks](/reference/rules/react-orchestrates-components-and-hooks)
27
-
*[Rules of Hooks](/reference/rules/rules-of-hooks)
28
-
*[Rules of JSX](/reference/rules/rules-of-jsx)
31
+
*[Rules of Hooks](/reference/rules/rules-of-hooks)
Copy file name to clipboardExpand all lines: src/content/reference/rules/react-orchestrates-components-and-hooks.md
+17-14
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: React orchestrates Components and Hooks
3
3
---
4
4
5
5
<Intro>
6
-
TODO
6
+
React takes care of when your code runs for you so that your application has a great user experience. It is declarative: you tell React what to render in your component’s logic, and React will figure out how best to display it to your user.
7
7
</Intro>
8
8
9
9
<InlineToc />
@@ -13,17 +13,15 @@ TODO
13
13
## Never call component functions directly {/*never-call-component-functions-directly*/}
14
14
Components should only be used in JSX. Don't call them as regular functions.
15
15
16
-
React takes care of _when_ your code runs for you so that your application has a great user experience. It is declarative: you tell React what to render in your component's logic, and React will figure out how best to display it to your user.
17
-
18
-
In order to do this, React must decide when your component function is called during rendering. In React, you do this using JSX.
16
+
React must decide when your component function is called during rendering. In React, you do this using JSX.
19
17
20
18
```js {2}
21
19
functionBlogPost() {
22
20
return<Layout><Article /></Layout>; // ✅ Only use components in JSX
23
21
}
24
22
```
25
23
26
-
```js {3}
24
+
```js {2}
27
25
functionBlogPost() {
28
26
return<Layout>{Article()}</Layout>// ❌ Never call them directly
29
27
}
@@ -47,28 +45,32 @@ Hooks allow you to augment a component with React features. They should always b
47
45
48
46
Passing around hooks as regular values also inhibits the compiler at performing optimizations. Breaking this rule will de-optimize your component.
49
47
48
+
### Don't dynamically mutate a hook {/*dont-dynamically-mutate-a-hook*/}
49
+
50
+
Hooks should be as "static" as possible. This means you shouldn't dynamically mutate them. For example, this means you shouldn't write higher order hooks:
Hooks should be immutable and not be mutated. Instead of mutating a hook dynamically, create a static version of the hook with the desired functionality.
60
+
61
+
```js {2,6}
58
62
functionChatInput() {
59
63
constdata=useDataWithLogging(); // ✅
60
64
}
61
-
```
62
-
63
-
Hooks should be immutable and not be mutated. Instead of mutating a hook dynamically, create a static version of the hook with the desired functionality.
64
65
65
-
```js
66
66
functionuseDataWithLogging() {
67
-
// ... Logic should go in here
67
+
// ... Create a new version of the Hook and inline the logic here
68
68
}
69
69
```
70
70
71
-
Hooks should also not be dynamically used: for example, instead of doing dependency injection in a component:
71
+
### Don't dynamically use hooks {/*dont-dynamically-use-hooks*/}
72
+
73
+
Hooks should also not be dynamically used: for example, instead of doing dependency injection in a component by passing a hook as a value:
72
74
73
75
```js {2}
74
76
functionChatInput() {
@@ -84,11 +86,12 @@ function ChatInput() {
84
86
}
85
87
86
88
functionButton() {
87
-
constdata=useDataWithLogging();
89
+
constdata=useDataWithLogging();// ✅
88
90
}
89
91
90
92
functionuseDataWithLogging() {
91
-
// conditional logic can live here
93
+
// If there's any conditional logic to change the hook's behavior, it should be inlined into
Copy file name to clipboardExpand all lines: src/content/reference/rules/rules-of-hooks.md
+1-4
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Rules of Hooks
3
3
---
4
4
5
5
<Intro>
6
-
TODO
6
+
Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce these rules automatically.
7
7
</Intro>
8
8
9
9
<InlineToc />
@@ -115,6 +115,3 @@ function setOnlineStatus() { // ❌ Not a component or custom hook!
0 commit comments