Measure layout with useLayoutEffect
Measure after the DOM commit, before paint
Use useLayoutEffect when a visible result depends on browser geometry and an intermediate position should not be painted. React first commits DOM nodes, then the layout effect can read them and schedule a correction before repaint. This blocks painting, so ordinary external synchronization belongs in useEffect and ordinary layout belongs in CSS. The examples target React 18 or 19 in a client app; they are independent App.jsx files and their visual outcomes are expected, not yet browser-tested.
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';export default function App(){const box=useRef(null);const [width,setWidth]=useState(null);useLayoutEffect(()=>{setWidth(box.current.getBoundingClientRect().width);},[]);return <section><div ref={box} style={{width:160,boxSizing:'border-box',border:'1px solid'}}>Exhibit A</div><p>Width: {width===null?'Measuring':width+' CSS pixels'}</p></section>;}The label uses a 160 CSS pixel border box. In a normal untransformed document the paragraph should show Width: 160 CSS pixels. getBoundingClientRect includes borders and reports viewport-relative geometry. The empty dependency list is sufficient for this deliberately fixed-size fixture, not for dynamic text or responsive sizes. Do not read a DOM node during render: it may not exist yet or reflect the next commit.
Position a small measured information popup
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function positionTip(anchor, tip, viewport, gap=8) { const above=anchor.top-tip.height-gap; const below=anchor.bottom+gap; const preferred=above>=gap?above:below; return {top:Math.max(gap,Math.min(preferred,viewport.height-tip.height-gap)),left:Math.max(gap,Math.min(anchor.left,viewport.width-tip.width-gap))};}function Tip({anchor}){const tip=useRef(null);const [pos,setPos]=useState(null);useLayoutEffect(()=>{function measure(){setPos(positionTip(anchor.current.getBoundingClientRect(),tip.current.getBoundingClientRect(),{width:document.documentElement.clientWidth,height:document.documentElement.clientHeight}));}measure();window.addEventListener('resize',measure);window.addEventListener('scroll',measure,true);return ()=>{window.removeEventListener('resize',measure);window.removeEventListener('scroll',measure,true);};},[anchor]);return <span ref={tip} id="exhibit-tip" style={{position:'fixed',top:pos?.top??0,left:pos?.left??0,maxWidth:'min(14rem, calc(100vw - 16px))',boxSizing:'border-box',padding:8,background:'#eee',color:'#111',visibility:pos?'visible':'hidden'}}>Clay bowl, gallery two.</span>;}export default function App(){const anchor=useRef(null);const [open,setOpen]=useState(false);return <section><button ref={anchor} aria-expanded={open} aria-controls={open?'exhibit-tip':undefined} onClick={()=>setOpen(v=>!v)} onKeyDown={e=>{if(e.key==='Escape')setOpen(false);}}>Exhibit information</button>{open&&<Tip anchor={anchor}/>}</section>;}Activate the button to show an information disclosure, positioned like a tooltip. The first commit supplies measurable nodes; the next state update supplies coordinates. A hidden node still participates in layout, unlike display:none. The helper prefers above if it fits, otherwise below, and clamps to the viewport. It assumes no transformed fixed-position ancestor and a popup small enough to fit. Scroll and resize listeners are removed on close, including the matching capture flag. This click-based disclosure is not a full hover/focus ARIA tooltip implementation. Production overlays need content-size observation, collision policies and browser/accessibility tests; CSS positioning is preferable when it meets the requirement.
Avoid server-only layout assumptions
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function Ruler(){const ref=useRef(null);const [width,setWidth]=useState(0);useLayoutEffect(()=>{setWidth(ref.current.offsetWidth);},[]);return <div ref={ref} style={{width:120}}>Measured: {width}</div>;}export default function App(){const [ready,setReady]=useState(false);useEffect(()=>setReady(true),[]);return ready?<Ruler/>:<p>Measurement available after loading.</p>;}A server has no browser layout, and effects do not execute during server rendering. Here server markup and the initial hydration render can agree on the fallback. After the client effect runs, the child mounts and measures its fixed 120-pixel box. This deliberately delays the child and does not guarantee a particular paint schedule for the passive effect. If a visible correction is acceptable, a normal effect may be simpler. StrictMode can rehearse effect setup and cleanup in development; never rely on a single setup. Practice: use the geometry helper to predict top and left for a button near each viewport edge, then verify the disclosure with keyboard, scrolling and resizing in an approved browser.