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/useEffect.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,9 +44,9 @@ function ChatRoom({ roomId }) {
44
44
45
45
#### Parameters {/*parameters*/}
46
46
47
-
* `setup`: The function with your Effect's logic. Your setup function may also optionally return a *cleanup* function. When your component is added to the DOM, React will run your setup function. After every re-render with changed dependencies, React will first run the cleanup function (if you provided it) with the old values, and then run your setup function with the new values. After your component is removed from the DOM, React will run your cleanup function.
47
+
* `setup`: The function with your Effect's logic. Your setup function may also optionally return a *cleanup* function. When your [component commits](/learn/render-and-commit#step-3-react-commits-changes-to-the-dom), React will run your setup function. After every commit with changed dependencies, React will first run the cleanup function (if you provided it) with the old values, and then run your setup function with the new values. After your component is removed from the DOM, React will run your cleanup function.
48
48
49
-
* **optional** `dependencies`: The list of all reactive values referenced inside of the `setup` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If you omit this argument, your Effect will re-run after every re-render of the component. [See the difference between passing an array of dependencies, an empty array, and no dependencies at all.](#examples-dependencies)
49
+
* **optional** `dependencies`: The list of all reactive values referenced inside of the `setup` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If you omit this argument, your Effect will re-run after every commit of the component. [See the difference between passing an array of dependencies, an empty array, and no dependencies at all.](#examples-dependencies)
50
50
51
51
#### Returns {/*returns*/}
52
52
@@ -107,14 +107,14 @@ You need to pass two arguments to `useEffect`:
107
107
**React calls your setup and cleanup functions whenever it's necessary, which may happen multiple times:**
108
108
109
109
1. Your <CodeStep step={1}>setup code</CodeStep> runs when your component is added to the page *(mounts)*.
110
-
2. After every re-render of your component where the <CodeStep step={3}>dependencies</CodeStep> have changed:
110
+
2. After every commit of your component where the <CodeStep step={3}>dependencies</CodeStep> have changed:
111
111
- First, your <CodeStep step={2}>cleanup code</CodeStep> runs with the old props and state.
112
112
- Then, your <CodeStep step={1}>setup code</CodeStep> runs with the new props and state.
113
113
3. Your <CodeStep step={2}>cleanup code</CodeStep> runs one final time after your component is removed from the page *(unmounts).*
114
114
115
115
**Let's illustrate this sequence for the example above.**
116
116
117
-
When the `ChatRoom` component above gets added to the page, it will connect to the chat room with the initial `serverUrl` and `roomId`. If either `serverUrl` or `roomId` change as a result of a re-render (say, if the user picks a different chat room in a dropdown), your Effect will *disconnect from the previous room, and connect to the next one.* When the `ChatRoom` component is removed from the page, your Effect will disconnect one last time.
117
+
When the `ChatRoom` component above gets added to the page, it will connect to the chat room with the initial `serverUrl` and `roomId`. If either `serverUrl` or `roomId` change as a result of a commit (say, if the user picks a different chat room in a dropdown), your Effect will *disconnect from the previous room, and connect to the next one.* When the `ChatRoom` component is removed from the page, your Effect will disconnect one last time.
118
118
119
119
**To [help you find bugs,](/learn/synchronizing-with-effects#step-3-add-cleanup-if-needed) in development React runs <CodeStep step={1}>setup</CodeStep> and <CodeStep step={2}>cleanup</CodeStep> one extra time before the <CodeStep step={1}>setup</CodeStep>.** This is a stress-test that verifies your Effect's logic is implemented correctly. If this causes visible issues, your cleanup function is missing some logic. The cleanup function should stop or undo whatever the setup function was doing. The rule of thumb is that the user shouldn't be able to distinguish between the setup being called once (as in production) and a *setup* → *cleanup* → *setup* sequence (as in development). [See common solutions.](/learn/synchronizing-with-effects#how-to-handle-the-effect-firing-twice-in-development)
120
120
@@ -1145,7 +1145,7 @@ useEffect(() => {
1145
1145
1146
1146
#### Passing a dependency array {/*passing-a-dependency-array*/}
1147
1147
1148
-
If you specify the dependencies, your Effect runs **after the initial render _and_ after re-renders with changed dependencies.**
1148
+
If you specify the dependencies, your Effect runs **after the initial commit _and_ after commits with changed dependencies.**
If your Effect depends on an object or a function created during rendering, it might run too often. For example, this Effect re-connects after every render because the `options` object is [different for every render:](/learn/removing-effect-dependencies#does-some-reactive-value-change-unintentionally)
1483
+
If your Effect depends on an object or a function created during rendering, it might run too often. For example, this Effect re-connects after every commit because the `options` object is [different for every render:](/learn/removing-effect-dependencies#does-some-reactive-value-change-unintentionally)
1484
1484
1485
1485
```js {6-9,12,15}
1486
1486
constserverUrl='https://localhost:1234';
@@ -1497,7 +1497,7 @@ function ChatRoom({ roomId }) {
1497
1497
constconnection=createConnection(options); // It's used inside the Effect
1498
1498
connection.connect();
1499
1499
return () =>connection.disconnect();
1500
-
}, [options]); // 🚩 As a result, these dependencies are always different on a re-render
1500
+
}, [options]); // 🚩 As a result, these dependencies are always different on a commit
1501
1501
// ...
1502
1502
```
1503
1503
@@ -1583,7 +1583,7 @@ With this fix, typing into the input doesn't reconnect the chat. Unlike an objec
1583
1583
1584
1584
### Removing unnecessary function dependencies {/*removing-unnecessary-function-dependencies*/}
1585
1585
1586
-
If your Effect depends on an object or a function created during rendering, it might run too often. For example, this Effect re-connects after every render because the `createOptions` function is [different for every render:](/learn/removing-effect-dependencies#does-some-reactive-value-change-unintentionally)
1586
+
If your Effect depends on an object or a function created during rendering, it might run too often. For example, this Effect re-connects after every commit because the `createOptions` function is [different for every render:](/learn/removing-effect-dependencies#does-some-reactive-value-change-unintentionally)
1587
1587
1588
1588
```js {4-9,12,16}
1589
1589
functionChatRoom({ roomId }) {
@@ -1601,11 +1601,11 @@ function ChatRoom({ roomId }) {
1601
1601
constconnection=createConnection();
1602
1602
connection.connect();
1603
1603
return () =>connection.disconnect();
1604
-
}, [createOptions]); // 🚩 As a result, these dependencies are always different on a re-render
1604
+
}, [createOptions]); // 🚩 As a result, these dependencies are always different on a commit
1605
1605
// ...
1606
1606
```
1607
1607
1608
-
By itself, creating a function from scratch on every re-render is not a problem. You don't need to optimize that. However, if you use it as a dependency of your Effect, it will cause your Effect to re-run after every re-render.
1608
+
By itself, creating a function from scratch on every re-render is not a problem. You don't need to optimize that. However, if you use it as a dependency of your Effect, it will cause your Effect to re-run after every commit.
1609
1609
1610
1610
Avoid using a function created during rendering as a dependency. Instead, declare it inside the Effect:
1611
1611
@@ -1775,7 +1775,7 @@ First, check that you haven't forgotten to specify the dependency array:
1775
1775
```js {3}
1776
1776
useEffect(() => {
1777
1777
// ...
1778
-
}); // 🚩 No dependency array: re-runs after every render!
1778
+
}); // 🚩 No dependency array: re-runs after every commit!
1779
1779
```
1780
1780
1781
1781
If you've specified the dependency array but your Effect still re-runs in a loop, it's because one of your dependencies is different on every re-render.
Copy file name to clipboardExpand all lines: src/content/reference/react/useLayoutEffect.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,9 +47,9 @@ function Tooltip() {
47
47
48
48
#### Parameters {/*parameters*/}
49
49
50
-
* `setup`: The function with your Effect's logic. Your setup function may also optionally return a *cleanup* function. Before your component is added to the DOM, React will run your setup function. After every re-render with changed dependencies, React will first run the cleanup function (if you provided it) with the old values, and then run your setup function with the new values. Before your component is removed from the DOM, React will run your cleanup function.
50
+
* `setup`: The function with your Effect's logic. Your setup function may also optionally return a *cleanup* function. Before your [component commits](/learn/render-and-commit#step-3-react-commits-changes-to-the-dom), React will run your setup function. After every commit with changed dependencies, React will first run the cleanup function (if you provided it) with the old values, and then run your setup function with the new values. Before your component is removed from the DOM, React will run your cleanup function.
51
51
52
-
* **optional** `dependencies`: The list of all reactive values referenced inside of the `setup` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If you omit this argument, your Effect will re-run after every re-render of the component.
52
+
* **optional** `dependencies`: The list of all reactive values referenced inside of the `setup` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If you omit this argument, your Effect will re-run after every commit of the component.
0 commit comments