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
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:
30
30
31
31
```js
32
32
import { useInsertionEffect } from'react';
@@ -44,7 +44,7 @@ function useCSS(rule) {
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. 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.
48
48
49
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 algorithm. If you don't specify the dependencies at all, your Effect will re-run after every re-render of the component.
50
50
@@ -56,8 +56,9 @@ function useCSS(rule) {
56
56
57
57
* Effects only run on the client. They don't run during server rendering.
58
58
* 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.
61
62
---
62
63
63
64
## Usage {/*usage*/}
@@ -87,7 +88,7 @@ If you use CSS-in-JS, we recommend a combination of the first two approaches (CS
87
88
88
89
The first problem is not solvable, but `useInsertionEffect` helps you solve the second problem.
89
90
90
-
Call `useInsertionEffect` to insert the styles before any DOM mutations:
91
+
Call `useInsertionEffect` to insert the styles before any layout effects fire:
0 commit comments