useImperativeHandle
The React hook that lets a component choose exactly which methods its parent gets through a ref, instead of the raw DOM node.
In more detail
You return an object such as { focus, clear } from a factory function and React assigns it to the parent ref. Parents can then call only these methods and cannot change the child DOM styles or listeners. It takes an optional dependency array like other hooks.
Example
useImperativeHandle(ref, () => ({ focus: () => inputRef.current?.focus() }));Related terms
- ref — A React handle that points at a real DOM element, so code can focus it, measure it or attach listeners.
- forwardRef — The older React API needed to let a function component receive a ref from its parent.
- encapsulation — Keeping a component's internal data and details behind a small public interface.
- imperative programming — A style of programming where you give explicit step-by-step commands, such as find this element and set its text.
Taught in
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- useImperativeHandle: Controlled Ref Forwarding Boundaries — React 19 & Modern Component Architecture