Skip to content

Commit caba227

Browse files
committed
Address feedback part 1
1 parent 8acb069 commit caba227

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

src/content/reference/react/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The React reference documentation is broken down into functional subsections:
1212

1313
## Rules of React {/*rules-of-react*/}
1414

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:
1616

1717
* [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.
1818
* [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.

src/content/reference/rules/components-and-hooks-must-be-pure.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ title: Components and Hooks must be pure
1010

1111
<DeepDive>
1212
#### 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.
1415

1516
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.
1617

@@ -21,23 +22,23 @@ Concretely, this means that rendering logic can be run multiple times in a way t
2122

2223
## Components must be idempotent {/*components-must-be-idempotent*/}
2324

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_.
2526

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.
2728

2829
```js {3}
2930
function NewsFeed({ items }) {
30-
// ✅ Array.filter doesn't mutate `items`
31+
// ✅ Array.filter is idempotent: it doesn't mutate `items`
3132
const filteredItems = items.filter(item => item.isDisplayed === true);
3233
return (
3334
<ul>
34-
{filteredItems.map(item => <li key={item.id}>{item.text}</li>}
35+
{filteredItems.map(item => <li key={item.id}>{item.text}</li>)}
3536
</ul>
3637
);
3738
}
3839
```
3940

40-
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):
4142

4243
```js {2}
4344
function Clock() {
@@ -101,7 +102,7 @@ function FriendList({ friends }) {
101102

102103
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**.
103104

104-
Similarly, lazy initialization is fine despite not being fully "pure":
105+
Lazy initialization is also fine despite not being fully "pure":
105106

106107
```js {2}
107108
function ExpenseForm() {
@@ -120,11 +121,11 @@ function ProductDetailPage({ product }) {
120121

121122
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).
122123

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).
124125

125126
## Props and state are immutable {/*props-and-state-are-immutable*/}
126127

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`.
128129

129130
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.
130131

src/content/reference/rules/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Rules of React
33
---
44

55
<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.
77
</Intro>
88

99
<InlineToc />

src/sidebarReference.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
"title": "API Reference",
33
"path": "/reference/react",
44
"routes": [
5-
{
6-
"hasSectionHeader": true,
7-
"sectionHeader": "[email protected]"
8-
},
9-
{
10-
"title": "Overview",
11-
"path": "/reference/react"
12-
},
135
{
146
"title": "Rules of React",
157
"path": "/reference/rules",
@@ -28,6 +20,14 @@
2820
}
2921
]
3022
},
23+
{
24+
"hasSectionHeader": true,
25+
"sectionHeader": "[email protected]"
26+
},
27+
{
28+
"title": "Overview",
29+
"path": "/reference/react"
30+
},
3131
{
3232
"title": "Hooks",
3333
"path": "/reference/react/hooks",

0 commit comments

Comments
 (0)