Expose a constrained imperative handle
Expose a small interface rather than a whole DOM node
useImperativeHandle lets a child decide what a parent receives through a ref. Expose actions such as focus or select when declarative props are not a good fit. Do not expose mutable state or a general DOM escape hatch by default. A parent should call the handle after commit, normally in an event handler. React 19 function components accept ref as a prop; React 18 requires forwardRef for this pattern.
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function SearchField({ref,label}){const input=useRef(null);useImperativeHandle(ref,()=>({focus(){input.current?.focus();}}),[]);return <label>{label} <input ref={input}/></label>;}export default function App(){const field=useRef(null);return <section><button onClick={()=>field.current?.focus()}>Focus catalogue search</button><SearchField ref={field} label="Catalogue search"/></section>;}Clicking the button should move keyboard focus to the labelled input. The parent receives an object with focus, not the underlying input element. The internal ref object has stable identity, and its current node is read when the method runs. Optional chaining handles the period before mounting or after removal. Keep value changes in props/state instead of adding imperative setters for everything.
Support the React 18 compatibility lane
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';const SearchField=forwardRef(function SearchField({label},ref){const input=useRef(null);useImperativeHandle(ref,()=>({focus(){input.current?.focus();},select(){input.current?.select();}}),[]);return <label>{label} <input ref={input} defaultValue="Gallery two"/></label>;});export default function App(){const field=useRef(null);return <section><button onClick={()=>field.current?.select()}>Select search text</button><button onClick={()=>field.current?.focus()}>Focus search</button><SearchField ref={field} label="Search"/></section>;}The select action selects the text; the focus action moves focus. forwardRef still supports legacy consumers in React 19 but is no longer needed for new React 19-only function components. Do not remove it from a library that still supports React 18. Verify both versions independently, including types and consumers. Migration changes the component signature, not the intended public handle.
Handle dependencies and callback ref cleanup
import React,{useState,useRef,useLayoutEffect,useEffect,useImperativeHandle,forwardRef,useInsertionEffect,useId,useDebugValue,useCallback} from 'react';function Field({ref,label}){const input=useRef(null);const attach=useCallback(node=>{if(!node)return;input.current=node;const observer=new ResizeObserver(()=>{ /* Library integration point: observe only. */ });observer.observe(node);return ()=>{observer.disconnect();input.current=null;};},[]);useImperativeHandle(ref,()=>({focus(){input.current?.focus();},describe(){return label;}}),[label]);return <label>{label} <input ref={attach}/></label>;}export default function App(){const field=useRef(null);const [label,setLabel]=useState('Search');const [show,setShow]=useState(true);const [message,setMessage]=useState('');return <section><button onClick={()=>setLabel(v=>v==='Search'?'Find':'Search')}>Change label</button><button onClick={()=>setShow(v=>!v)}>Toggle field</button><button onClick={()=>setMessage(field.current?.describe()??'No field')}>Read label</button>{show&&<Field ref={field} label={label}/>}<p role="status">{message}</p></section>;}Read label reports Search or Find according to the current label. Changing label must recreate the handle because describe captures it. Hiding the field releases the observer and clears the parent handle; Read label then reports No field. The returned callback-ref cleanup is a React 19 feature; React 18 code should handle the null callback and release the old observer there instead. The observer is intentionally an integration skeleton, not a responsive component. A stable callback avoids needless detach/attach work. In development, StrictMode can repeat ref setup and cleanup; check resource balance rather than exact counts. Practice: add a select method without exposing the node, and test removal and remount.