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/react/index.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ The React reference documentation is broken down into functional subsections:
12
12
13
13
## Rules of React {/*rules-of-react*/}
14
14
15
-
React is a programming paradigm for declarative user interfaces, in which developers describe what should be rendered given the current state and the framework is responsible for updating the UI. Just as different programming languages have their own ways of expressing concepts, React has its own idioms — or rules — for how to express patterns in a way that is easy to understand and yields high-quality applications.
15
+
React has idioms — or rules — for how to express patterns in a way that is easy to understand and yields high-quality applications:
16
16
17
17
*[Components and Hooks must be pure](/reference/rules/components-and-hooks-must-be-pure) – Purity makes your code easier to understand, debug, and allows React to automatically optimize your components and hooks correctly.
18
18
*[React orchestrates Components and Hooks](/reference/rules/react-orchestrates-components-and-hooks) – React is responsible for rendering components and hooks when necessary to optimize the user experience.
Copy file name to clipboardExpand all lines: src/content/reference/rules/components-and-hooks-must-be-pure.md
+10-9
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,8 @@ title: Components and Hooks must be pure
10
10
11
11
<DeepDive>
12
12
#### Why does render need to be pure? {/*why-does-render-need-to-be-pure*/}
13
-
UI libraries like React take care of when your code runs for you so that your application has a great user experience. React 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!
13
+
14
+
React 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.
14
15
15
16
When render is kept pure, React can understand how to prioritize which updates are most important for the user to see first. This is made possible because of render purity: since components don't have side effects in render, React can pause rendering components that aren't as important to update, and only come back to them later when it's needed.
16
17
@@ -21,23 +22,23 @@ Concretely, this means that rendering logic can be run multiple times in a way t
21
22
22
23
## Components must be idempotent {/*components-must-be-idempotent*/}
23
24
24
-
React assumes that components always return the same output with respect to their inputs – props, state, and context. This is known as [idempotency](https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation).
25
+
Components must always return the same output with respect to their inputs – props, state, and context. This is known as _idempotency_.
25
26
26
-
Put simply, idempotence means that you [always get the same result everytime](learn/keeping-components-pure) you run that piece of code.
27
+
[Idempotency](https://en.wikipedia.org/wiki/Idempotence) is a term popularized in functional programming. It refers to the idea that you [always get the same result everytime](learn/keeping-components-pure) you run that piece of code with the same inputs.
27
28
28
29
```js {3}
29
30
functionNewsFeed({ items }) {
30
-
// ✅ Array.filter doesn't mutate `items`
31
+
// ✅ Array.filter is idempotent: it doesn't mutate `items`
This means that _all_ code that runs during render must also be idempotent in order for this rule to hold. For example, this line of code is not idempotent (and therefore, neither is the component) and breaks this rule:
41
+
This means that _all_ code that runs during render must also be idempotent in order for this rule to hold. For example, this line of code is not idempotent (and therefore, neither is the component):
41
42
42
43
```js {2}
43
44
functionClock() {
@@ -101,7 +102,7 @@ function FriendList({ friends }) {
101
102
102
103
When `<FriendList />` runs again, we will continue appending `friends` to `items` every time that component is run, leading to multiple duplicated results. This version of `<FriendList />` has observable side effects during render and **breaks the rule**.
103
104
104
-
Similarly, lazy initialization is fine despite not being fully "pure":
105
+
Lazy initialization is also fine despite not being fully "pure":
105
106
106
107
```js {2}
107
108
functionExpenseForm() {
@@ -120,11 +121,11 @@ function ProductDetailPage({ product }) {
120
121
121
122
One way to achieve the desired result of updating `window.title` outside of render is to [synchronize the component with `window`](http://localhost:3000/learn/synchronizing-with-effects).
122
123
123
-
As long as calling a component multiple times is safe and doesn’t affect the rendering of other components, React doesn’t care if it’s 100% pure in the strict functional programming sense of the word. It is more important that [components must be idempotent](/reference/rules/components-must-be-idempotent).
124
+
As long as calling a component multiple times is safe and doesn’t affect the rendering of other components, React doesn’t care if it’s 100% pure in the strict functional programming sense of the word. It is more important that [components must be idempotent](/reference/rules/components-and-hooks-must-be-pure).
124
125
125
126
## Props and state are immutable {/*props-and-state-are-immutable*/}
126
127
127
-
A component's props and state are immutable [snapshots](learn/state-as-a-snapshot) with respect to a single render. Never mutate them directly.
128
+
A component's props and state are immutable [snapshots](learn/state-as-a-snapshot). Never mutate them directly. Instead, pass new props down, and use the setter function from `useState`.
128
129
129
130
You can think of the props and state values as snapshots that are updated after rendering. For this reason, you don't modify the props or state variables directly: instead you pass new props, or use the setter function provided to you to tell React that state needs to update the next time the component is rendered.
Copy file name to clipboardExpand all lines: src/content/reference/rules/index.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Rules of React
3
3
---
4
4
5
5
<Intro>
6
-
React is a programming paradigm for declarative user interfaces, in which developers describe what should be rendered given the current state and the framework is responsible for updating the UI. Just as different programming languages have their own ways of expressing concepts, React has its own idioms — or rules — for how to express patterns in a way that is easy to understand and yields high-quality applications. This page lists all the Rules of React.
6
+
Just as different programming languages have their own ways of expressing concepts, React has its own idioms — or rules — for how to express patterns in a way that is easy to understand and yields high-quality applications. This page lists all the Rules of React.
0 commit comments