Skip to content

Commit d86cfc4

Browse files
authored
Update useInsertionEffect docs (#6172)
1 parent b9eea4d commit d86cfc4

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/content/reference/react/useInsertionEffect.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ title: useInsertionEffect
1010

1111
<Intro>
1212

13-
`useInsertionEffect` is a version of [`useEffect`](/reference/react/useEffect) that fires before any DOM mutations.
13+
`useInsertionEffect` allows inserting elements into the DOM before any layout effects fire.
1414

1515
```js
1616
useInsertionEffect(setup, dependencies?)
@@ -26,7 +26,7 @@ useInsertionEffect(setup, dependencies?)
2626
2727
### `useInsertionEffect(setup, dependencies?)` {/*useinsertioneffect*/}
2828
29-
Call `useInsertionEffect` to insert the styles before any DOM mutations:
29+
Call `useInsertionEffect` to insert styles before any effects fire that may need to read layout:
3030
3131
```js
3232
import { useInsertionEffect } from 'react';
@@ -44,7 +44,7 @@ function useCSS(rule) {
4444
4545
#### Parameters {/*parameters*/}
4646
47-
* `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.
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, but before any layout effects fire, 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. When your component is removed from the DOM, React will run your cleanup function.
4848
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 algorithm. If you don't specify the dependencies at all, your Effect will re-run after every re-render of the component.
5050
@@ -56,8 +56,9 @@ function useCSS(rule) {
5656
5757
* Effects only run on the client. They don't run during server rendering.
5858
* You can't update state from inside `useInsertionEffect`.
59-
* By the time `useInsertionEffect` runs, refs are not attached yet, and DOM is not yet updated.
60-
59+
* By the time `useInsertionEffect` runs, refs are not attached yet.
60+
* `useInsertionEffect` may run either before or after the DOM has been updated. You shouldn't rely on the DOM being updated at any particular time.
61+
* Unlike other types of Effects, which fire cleanup for every Effect and then setup for every Effect, `useInsertionEffect` will fire both cleanup and setup one component at a time. This results in an "interleaving" of the cleanup and setup functions.
6162
---
6263
6364
## Usage {/*usage*/}
@@ -87,7 +88,7 @@ If you use CSS-in-JS, we recommend a combination of the first two approaches (CS
8788
8889
The first problem is not solvable, but `useInsertionEffect` helps you solve the second problem.
8990
90-
Call `useInsertionEffect` to insert the styles before any DOM mutations:
91+
Call `useInsertionEffect` to insert the styles before any layout effects fire:
9192
9293
```js {4-11}
9394
// Inside your CSS-in-JS library

0 commit comments

Comments
 (0)