useInsertionEffect is a version of useEffect that fires before any DOM mutations.
Parameters
() => (() => void) | void
required
The function with your Effect’s logic. Your effect function can optionally return a cleanup function.
- React will call your effect function before any DOM mutations
- React will call your cleanup function before the component is removed
- If dependencies change, React will run cleanup, then run your effect with new values
Array<mixed> | void | null
The list of all reactive values referenced inside the effect code. Works the same as
useEffect dependencies.- If omitted, the effect runs after every render
- If
[](empty array), the effect runs only once (on mount) - If
[dep1, dep2], the effect runs when any dependency changes
Returns
useInsertionEffect returns undefined.
Usage
Injecting dynamic styles from CSS-in-JS libraries
Traditionally, you would style React components using plain CSS:- Static extraction to CSS files with a compiler
- Inline styles:
<div style={{ ... }}> - Runtime injection of
<style>tags
useInsertionEffect:
useInsertionEffect is better than injecting styles during render or in useLayoutEffect because it ensures that by the time other Effects run in your components, the <style> tags have already been injected.How CSS-in-JS libraries use this
Basic style injection
CSS-in-JS with dynamic values
Styled components pattern
Effect Timing Comparison
- useInsertionEffect
- useLayoutEffect
- useEffect
<style> tags for CSS-in-JSTypeScript
Troubleshooting
Why not just use useEffect or useLayoutEffect?
Can I read DOM in useInsertionEffect?
No, you cannot read from the DOM inuseInsertionEffect because the DOM hasn’t been updated yet:
I’m not building a CSS-in-JS library - do I need this?
No! Unless you’re authoring a CSS-in-JS library, you should use:useEffectfor most side effectsuseLayoutEffectfor measuring DOM or synchronous mutations- Plain CSS or CSS modules for styling
Best Practices
Only for CSS-in-JS libraries
useInsertionEffect is a specialized hook for library authors:
Prevent duplicate injections
Clean up injected styles
When to use useInsertionEffect
UseuseInsertionEffect when:
- ✅ Building a CSS-in-JS library
- ✅ Injecting
<style>tags dynamically - ✅ Need to inject styles before layout is calculated
useLayoutEffect when:
- ✅ Measuring DOM elements
- ✅ Reading layout (getBoundingClientRect, offsetHeight, etc.)
- ✅ Synchronous DOM mutations before paint
useEffect when:
- ✅ Everything else!
- ✅ Data fetching
- ✅ Setting up subscriptions
- ✅ Logging and analytics
- ✅ Most side effects