useImperativeHandle is a React Hook that lets you customize the handle exposed as a ref.
Parameters
Ref<T>
required
The
ref you received from the forwardRef render function as the second argument.() => T
required
A function that takes no arguments and returns the ref handle you want to expose. The ref handle can be of any type. Usually, you will return an object with the methods you want to expose.
Array<mixed> | void | null
The list of all reactive values referenced inside the
create code. React will re-run create and update the ref when dependencies change.- If omitted, the handle is recreated on every render
- If
[](empty array), the handle is created once - If
[dep1, dep2], the handle updates when dependencies change
Returns
useImperativeHandle returns undefined.
Usage
Exposing a custom ref handle to the parent component
By default, components don’t expose their DOM nodes to parent components. You can opt into this by usingforwardRef and useImperativeHandle:
Exposing specific methods
Limit the exposed API to only what the parent needs:Common Patterns
Custom input component
Form handle
Modal handle
With dependencies
TypeScript
Generic component with ref
Troubleshooting
My ref is null in the parent
Make sure you’re usingforwardRef:
The handle is recreated on every render
Provide a dependency array:When should I use useImperativeHandle?
UseuseImperativeHandle when:
- ✅ You need to expose imperative methods (focus, scroll, play/pause)
- ✅ You want to hide the internal implementation
- ✅ You need to expose a subset of functionality
useImperativeHandle when:
- ❌ You can use props instead
- ❌ You’re exposing too many methods (component is doing too much)
- ❌ The parent should control state (use props)