Focus an input after an event
Give focus to an owned DOM element
Use an object ref initialized to null, then attach it to the JSX element. React assigns the DOM node during commit and clears the reference when the node detaches. Read it in an event callback or appropriate Effect, not while calculating JSX. Focus is a browser operation, not a React state update. Provide visible labels and use native buttons to let keyboard users trigger it.
import { useRef } from 'react';export default function App() { const search = useRef(null); return <main><h1>Sample catalogue</h1> <button onClick={() => search.current?.focus()}>Jump to search</button> <label htmlFor="sample-search">Sample name</label> <input id="sample-search" ref={search} type="search" /> </main>;}Activating Jump to search should place keyboard focus in the labeled input. Optional chaining avoids a method call when there is no attached element. Do not remove the browser focus outline. Ref access should remain limited to the intended element.
Handle an element that is not always mounted
import { useEffect, useRef, useState } from 'react';export default function App() { const [open, setOpen] = useState(false); const field = useRef(null); const opener = useRef(null); useEffect(() => { if (open) field.current?.focus(); }, [open]); function close() { setOpen(false); opener.current?.focus(); } return <main><h1>Sample notes</h1> <button ref={opener} aria-expanded={open} onClick={() => setOpen(true)}>Open notes</button> {open && <section><label htmlFor="sample-note">Note</label> <input id="sample-note" ref={field} /> <button onClick={close}>Close notes</button></section>} </main>;}Calling focus immediately after setOpen(true) may be too early because the element has not been committed. This example synchronizes focus after the open update. Close returns focus to the still-mounted opener. This is an inline disclosure, not a modal dialog; no focus trap is needed. The effect runs when open changes, not for each keystroke. Avoid automatic focus on unrelated rerenders.
Measure an element when the user asks
import { useRef, useState } from 'react';export default function App() { const card = useRef(null); const [width, setWidth] = useState(null); function measure() { const node = card.current; if (node) setWidth(Math.round(node.getBoundingClientRect().width)); } return <main><h1>Sample card measurement</h1> <div ref={card} style={{width: 'min(240px, 100%)', boxSizing: 'border-box', border: '1px solid'}}>Quartz</div> <button onClick={measure}>Measure card</button> <p role="status">{width === null ? 'Not measured' : width + ' CSS px'}</p> </main>;}Measure card captures the current bounding rectangle width in CSS pixels, rounded for display. In a sufficiently wide viewport the untransformed card should be 240 CSS px; a narrower container can produce a smaller result. Resizing does not automatically update the captured value. Continuous observation needs a separate lifetime and cleanup (for example ResizeObserver, a later browser integration topic). Avoid imperatively changing React-owned children; express content changes using props or state.
Practice
Explain why the notes field is null before mounting. In the approved browser, open notes with the keyboard, type, close, and verify focus returns. Then measure the card in a narrow and a wide viewport.