File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
src/content/reference/react Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,36 @@ function useCSS(rule) {
6262---
6363
6464## Usage {/*usage*/}
65+ ### A simple example: inserting a style before the page paints {/*simple-example*/}
66+
67+ If you’re new to ` useInsertionEffect` , here’s a minimal example that shows when it runs.
68+ It injects a ` < style> ` tag before the browser paints the UI, ensuring styles are applied immediately.
69+
70+ ` ` ` js
71+ import { useInsertionEffect } from ' react' ;
72+
73+ export default function HighlightExample () {
74+ useInsertionEffect (() => {
75+ const style = document .createElement (' style' );
76+ style .textContent = `
77+ body {
78+ background-color: #f0f8ff;
79+ }
80+ ` ;
81+ document .head .appendChild (style);
82+ return () => {
83+ document .head .removeChild (style);
84+ };
85+ }, []);
86+
87+ return (
88+ < p>
89+ The background color is applied before the component paints!
90+ < / p>
91+ );
92+ }
93+ This small example helps visualize that useInsertionEffect runs before any layout or regular effects,
94+ making it useful for cases where styles or DOM insertions must happen very early in the render lifecycle.
6595
6696### Injecting dynamic styles from CSS - in - JS libraries {/* injecting-dynamic-styles-from-css-in-js-libraries*/ }
6797
You can’t perform that action at this time.
0 commit comments