Skip to content

Commit 3cccb66

Browse files
committed
Solve Confusion about executing timing of useEffect
1 parent ab18d2f commit 3cccb66

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/content/reference/react/useEffect.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function ChatRoom({ roomId }) {
4545
#### Parameters {/*parameters*/}
4646
4747
* `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.
48-
48+
4949
* **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)
5050
5151
#### Returns {/*returns*/}
@@ -66,7 +66,7 @@ function ChatRoom({ roomId }) {
6666
6767
* If your Effect is caused by an interaction (like a click), **React may run your Effect before the browser paints the updated screen**. This ensures that the result of the Effect can be observed by the event system. Usually, this works as expected. However, if you must defer the work until after paint, such as an `alert()`, you can use `setTimeout`. See [reactwg/react-18/128](https://github.com/reactwg/react-18/discussions/128) for more information.
6868
69-
* Even if your Effect was caused by an interaction (like a click), **React may allow the browser to repaint the screen before processing the state updates inside your Effect.** Usually, this works as expected. However, if you must block the browser from repainting the screen, you need to replace `useEffect` with [`useLayoutEffect`.](/reference/react/useLayoutEffect)
69+
* Even if your Effect was caused by an interaction (like a click), **React may allow the browser to repaint the screen before executing the code inside your Effect that responds to state updates.** Usually, this works as expected. However, if you must block the browser from repainting the screen, you need to replace `useEffect` with [`useLayoutEffect`.](/reference/react/useLayoutEffect)
7070
7171
* Effects **only run on the client.** They don't run during server rendering.
7272
@@ -112,7 +112,7 @@ You need to pass two arguments to `useEffect`:
112112
- Then, your <CodeStep step={1}>setup code</CodeStep> runs with the new props and state.
113113
3. Your <CodeStep step={2}>cleanup code</CodeStep> runs one final time after your component is removed from the page *(unmounts).*
114114
115-
**Let's illustrate this sequence for the example above.**
115+
**Let's illustrate this sequence for the example above.**
116116
117117
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.
118118
@@ -1081,7 +1081,7 @@ If either `serverUrl` or `roomId` change, your Effect will reconnect to the chat
10811081
```js {8}
10821082
function ChatRoom({ roomId }) {
10831083
const [serverUrl, setServerUrl] = useState('https://localhost:1234');
1084-
1084+
10851085
useEffect(() => {
10861086
const connection = createConnection(serverUrl, roomId);
10871087
connection.connect();
@@ -1435,7 +1435,7 @@ function Counter() {
14351435
}
14361436
```
14371437
1438-
Since `count` is a reactive value, it must be specified in the list of dependencies. However, that causes the Effect to cleanup and setup again every time the `count` changes. This is not ideal.
1438+
Since `count` is a reactive value, it must be specified in the list of dependencies. However, that causes the Effect to cleanup and setup again every time the `count` changes. This is not ideal.
14391439
14401440
To fix this, [pass the `c => c + 1` state updater](/reference/react/useState#updating-state-based-on-the-previous-state) to `setCount`:
14411441

0 commit comments

Comments
 (0)