Skip to main content
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 using forwardRef and useImperativeHandle:
Do not overuse refs. You should only use refs for imperative behaviors that you can’t express as props: scrolling to a node, focusing a node, triggering an animation, selecting text, and so on.If you can express something as a prop, you should not use a ref. For example, instead of exposing an imperative handle like { open, close } from a Modal component, it’s better to take isOpen as a prop like <Modal isOpen={isOpen} />.

Exposing specific methods

Limit the exposed API to only what the parent needs:

Common Patterns

Custom input component

Form handle

With dependencies

TypeScript

Generic component with ref

Troubleshooting

My ref is null in the parent

Make sure you’re using forwardRef:

The handle is recreated on every render

Provide a dependency array:

When should I use useImperativeHandle?

Use useImperativeHandle 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
Avoid 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)

Best Practices

Keep the handle simple

Name methods clearly

Document the ref API