Skip to content

Commit 6d849c9

Browse files
authored
Merge pull request #64 from reactjs/sync-c003ac4e
Sync with react.dev @ c003ac4
2 parents 5c92d97 + 83f0701 commit 6d849c9

File tree

8 files changed

+94
-12
lines changed

8 files changed

+94
-12
lines changed

.github/workflows/analyze.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ jobs:
1111
analyze:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v3
1515

1616
- name: Set up node
17-
uses: actions/setup-node@v1
17+
uses: actions/setup-node@v3
1818
with:
1919
node-version: '20.x'
2020

2121
- name: Install dependencies
2222
uses: bahmutov/[email protected]
2323

2424
- name: Restore next build
25-
uses: actions/cache@v2
25+
uses: actions/cache@v3
2626
id: restore-build-cache
2727
env:
2828
cache-name: cache-next-build
@@ -41,7 +41,7 @@ jobs:
4141
run: npx -p [email protected] report
4242

4343
- name: Upload bundle
44-
uses: actions/upload-artifact@v2
44+
uses: actions/upload-artifact@v3
4545
with:
4646
path: .next/analyze/__bundle_analysis.json
4747
name: bundle_analysis.json
@@ -73,7 +73,7 @@ jobs:
7373
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare
7474

7575
- name: Upload analysis comment
76-
uses: actions/upload-artifact@v2
76+
uses: actions/upload-artifact@v3
7777
with:
7878
name: analysis_comment.txt
7979
path: .next/analyze/__bundle_analysis_comment.txt
@@ -82,7 +82,7 @@ jobs:
8282
run: echo ${{ github.event.number }} > ./pr_number
8383

8484
- name: Upload PR number
85-
uses: actions/upload-artifact@v2
85+
uses: actions/upload-artifact@v3
8686
with:
8787
name: pr_number
8888
path: ./pr_number

src/content/learn/you-might-not-need-an-effect.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function Game() {
408408
409409
There are two problems with this code.
410410
411-
First problem is that it is very inefficient: the component (and its children) have to re-render between each `set` call in the chain. In the example above, in the worst case (`setCard` → render → `setGoldCardCount` → render → `setRound` → render → `setIsGameOver` → render) there are three unnecessary re-renders of the tree below.
411+
The first problem is that it is very inefficient: the component (and its children) have to re-render between each `set` call in the chain. In the example above, in the worst case (`setCard` → render → `setGoldCardCount` → render → `setRound` → render → `setIsGameOver` → render) there are three unnecessary re-renders of the tree below.
412412
413413
The second problem is that even if it weren't slow, as your code evolves, you will run into cases where the "chain" you wrote doesn't fit the new requirements. Imagine you are adding a way to step through the history of the game moves. You'd do it by updating each state variable to a value from the past. However, setting the `card` state to a value from the past would trigger the Effect chain again and change the data you're showing. Such code is often rigid and fragile.
414414

src/content/reference/react/useCallback.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ function ChatRoom({ roomId }) {
711711

712712
useEffect(() => {
713713
const options = createOptions();
714-
const connection = createConnection();
714+
const connection = createConnection(options);
715715
connection.connect();
716716
// ...
717717
```
@@ -722,7 +722,7 @@ This creates a problem. [Every reactive value must be declared as a dependency o
722722
```js {6}
723723
useEffect(() => {
724724
const options = createOptions();
725-
const connection = createConnection();
725+
const connection = createConnection(options);
726726
connection.connect();
727727
return () => connection.disconnect();
728728
}, [createOptions]); // 🔴 Problem: This dependency changes on every render
@@ -744,7 +744,7 @@ function ChatRoom({ roomId }) {
744744

745745
useEffect(() => {
746746
const options = createOptions();
747-
const connection = createConnection();
747+
const connection = createConnection(options);
748748
connection.connect();
749749
return () => connection.disconnect();
750750
}, [createOptions]); // ✅ Only changes when createOptions changes
@@ -766,7 +766,7 @@ function ChatRoom({ roomId }) {
766766
}
767767

768768
const options = createOptions();
769-
const connection = createConnection();
769+
const connection = createConnection(options);
770770
connection.connect();
771771
return () => connection.disconnect();
772772
}, [roomId]); // ✅ Only changes when roomId changes

src/content/reference/react/useMemo.md

+77
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,83 @@ Keep in mind that you need to run React in production mode, disable [React Devel
10561056

10571057
---
10581058

1059+
### Preventing an Effect from firing too often {/*preventing-an-effect-from-firing-too-often*/}
1060+
1061+
Sometimes, you might want to use a value inside an [Effect:](/learn/synchronizing-with-effects)
1062+
1063+
```js {4-7,10}
1064+
function ChatRoom({ roomId }) {
1065+
const [message, setMessage] = useState('');
1066+
1067+
const options = {
1068+
serverUrl: 'https://localhost:1234',
1069+
roomId: roomId
1070+
}
1071+
1072+
useEffect(() => {
1073+
const connection = createConnection(options);
1074+
connection.connect();
1075+
// ...
1076+
```
1077+
1078+
This creates a problem. [Every reactive value must be declared as a dependency of your Effect.](/learn/lifecycle-of-reactive-effects#react-verifies-that-you-specified-every-reactive-value-as-a-dependency) However, if you declare `options` as a dependency, it will cause your Effect to constantly reconnect to the chat room:
1079+
1080+
1081+
```js {5}
1082+
useEffect(() => {
1083+
const connection = createConnection(options);
1084+
connection.connect();
1085+
return () => connection.disconnect();
1086+
}, [options]); // 🔴 Problem: This dependency changes on every render
1087+
// ...
1088+
```
1089+
1090+
To solve this, you can wrap the object you need to call from an Effect in `useMemo`:
1091+
1092+
```js {4-9,16}
1093+
function ChatRoom({ roomId }) {
1094+
const [message, setMessage] = useState('');
1095+
1096+
const options = useMemo(() => {
1097+
return {
1098+
serverUrl: 'https://localhost:1234',
1099+
roomId: roomId
1100+
};
1101+
}, [roomId]); // ✅ Only changes when roomId changes
1102+
1103+
useEffect(() => {
1104+
const options = createOptions();
1105+
const connection = createConnection(options);
1106+
connection.connect();
1107+
return () => connection.disconnect();
1108+
}, [options]); // ✅ Only changes when createOptions changes
1109+
// ...
1110+
```
1111+
1112+
This ensures that the `options` object is the same between re-renders if `useMemo` returns the cached object.
1113+
1114+
However, since `useMemo` is performance optimization, not a semantic guarantee, React may throw away the cached value if [there is a specific reason to do that](#caveats). This will also cause the effect to re-fire, **so it's even better to remove the need for a function dependency** by moving your object *inside* the Effect:
1115+
1116+
```js {5-8,13}
1117+
function ChatRoom({ roomId }) {
1118+
const [message, setMessage] = useState('');
1119+
1120+
useEffect(() => {
1121+
const options = { // ✅ No need for useMemo or object dependencies!
1122+
serverUrl: 'https://localhost:1234',
1123+
roomId: roomId
1124+
}
1125+
1126+
const connection = createConnection(options);
1127+
connection.connect();
1128+
return () => connection.disconnect();
1129+
}, [roomId]); // ✅ Only changes when roomId changes
1130+
// ...
1131+
```
1132+
1133+
Now your code is simpler and doesn't need `useMemo`. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
1134+
1135+
10591136
### Memoizing a dependency of another Hook {/*memoizing-a-dependency-of-another-hook*/}
10601137
10611138
Suppose you have a calculation that depends on an object created directly in the component body:

src/content/reference/react/useReducer.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function MyComponent() {
5252
#### Caveats {/*caveats*/}
5353
5454
* `useReducer` is a Hook, so you can only call it **at the top level of your component** or your own Hooks. You can't call it inside loops or conditions. If you need that, extract a new component and move the state into it.
55+
* The `dispatch` function has a stable identity, so you will often see it omitted from effect dependencies, but including it will not cause the effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
5556
* In Strict Mode, React will **call your reducer and initializer twice** in order to [help you find accidental impurities.](#my-reducer-or-initializer-function-runs-twice) This is development-only behavior and does not affect production. If your reducer and initializer are pure (as they should be), this should not affect your logic. The result from one of the calls is ignored.
5657
5758
---

src/content/reference/react/useState.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function handleClick() {
8585
8686
* React [batches state updates.](/learn/queueing-a-series-of-state-updates) It updates the screen **after all the event handlers have run** and have called their `set` functions. This prevents multiple re-renders during a single event. In the rare case that you need to force React to update the screen earlier, for example to access the DOM, you can use [`flushSync`.](/reference/react-dom/flushSync)
8787
88+
* The `set` function has a stable identity, so you will often see it omitted from effect dependencies, but including it will not cause the effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
89+
8890
* Calling the `set` function *during rendering* is only allowed from within the currently rendering component. React will discard its output and immediately attempt to render it again with the new state. This pattern is rarely needed, but you can use it to **store information from the previous renders**. [See an example below.](#storing-information-from-previous-renders)
8991
9092
* In Strict Mode, React will **call your updater function twice** in order to [help you find accidental impurities.](#my-initializer-or-updater-function-runs-twice) This is development-only behavior and does not affect production. If your updater function is pure (as it should be), this should not affect the behavior. The result from one of the calls will be ignored.

src/content/reference/react/useTransition.md

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ function TabContainer() {
8080

8181
* The function you pass to `startTransition` must be synchronous. React immediately executes this function, marking all state updates that happen while it executes as Transitions. If you try to perform more state updates later (for example, in a timeout), they won't be marked as Transitions.
8282

83+
* The `startTransition` function has a stable identity, so you will often see it omitted from effect dependencies, but including it will not cause the effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
84+
8385
* A state update marked as a Transition will be interrupted by other state updates. For example, if you update a chart component inside a Transition, but then start typing into an input while the chart is in the middle of a re-render, React will restart the rendering work on the chart component after handling the input update.
8486

8587
* Transition updates can't be used to control text inputs.

src/content/reference/rsc/server-actions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ When React renders the `EmptyNote` Server Component, it will create a reference
5353
export default function Button({onClick}) {
5454
console.log(onClick);
5555
// {$$typeof: Symbol.for("react.server.reference"), $$id: 'createNoteAction'}
56-
return <button onClick={onClick}>Create Empty Note</button>
56+
return <button onClick={() => onClick()}>Create Empty Note</button>
5757
}
5858
```
5959

0 commit comments

Comments
 (0)