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

Taught in

References