Explain insertion effects, debug labels and IDs
Use library hooks only for their intended jobs
useInsertionEffect is an advanced client hook for style-injection libraries that need styles in place before layout-reading effects. It is not a replacement for CSS imports, useEffect or useLayoutEffect. Do not measure DOM nodes, rely on DOM update timing, read attached refs or schedule state from this hook. Server rendering needs a separate style collection strategy. Prefer static styles in ordinary applications.
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function Highlight({strong}){useInsertionEffect(()=>{const style=document.createElement('style');style.textContent=strong?'.bm-v33-exhibit { font-weight: 700; }':'.bm-v33-exhibit { font-weight: 400; }';document.head.appendChild(style);return ()=>style.remove();},[strong]);return <p className="bm-v33-exhibit">Featured exhibit</p>;}export default function App(){const [strong,setStrong]=useState(false);const [show,setShow]=useState(true);return <section><button onClick={()=>setStrong(v=>!v)}>Toggle emphasis</button><button onClick={()=>setShow(v=>!v)}>Toggle exhibit</button>{show&&<Highlight strong={strong}/>}</section>;}Toggle emphasis changes font weight; hiding the exhibit removes its inserted style. This deliberately supports one Highlight instance in an isolated client fixture, not a production CSS-in-JS engine. Multiple instances would need scoped rules or reference-counted deduplication. No user input becomes CSS; a real CSP may reject inline styles. The insertion effect may run before or after DOM changes; its cleanup/setup order differs from other effects. Do not infer server-side styles from this client code.
Generate accessible relationship IDs
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function Field({label,hint}){const id=useId();return <div><label htmlFor={id}>{label}</label><input id={id} aria-describedby={id+'-hint'}/><p id={id+'-hint'}>{hint}</p></div>;}export default function App(){return <section><Field label="Exhibit title" hint="Use a short title."/><Field label="Gallery name" hint="Use the room name."/></section>;}Both inputs should have distinct IDs and their own labels and descriptions. useId is designed to keep ID relationships consistent across compatible server and hydration renders with the same component tree. For multiple roots configure distinct identifierPrefix values, matching server and client for hydrated roots. Do not use generated IDs as list keys, secrets or persistent record identifiers; use stable data IDs for keys. useId is not available in async Server Components at the documented baseline. Avoid depending on the exact ID string.
Label custom hooks in developer tools
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function useDisplayMode(){const [compact,setCompact]=useState(false);useDebugValue(compact,value=>value?'Compact':'Comfortable');return [compact,()=>setCompact(v=>!v)];}export default function App(){const [compact,toggle]=useDisplayMode();return <section><button onClick={toggle}>Toggle display mode</button><p style={{padding:compact?4:20}}>{compact?'Compact':'Comfortable'}</p></section>;}The button changes the visible text and spacing. In React DevTools the custom hook can display a readable label; useDebugValue does not itself produce DOM output. The optional formatter lets DevTools calculate the label when inspecting the hook. Do not put sensitive data into debug labels. Prefer this hook for reusable abstractions where diagnostics help. Practice: render three fields, verify each label targets the correct input, and confirm style cleanup separately using the checklist.