useImperativeHandle customizes the instance value that is exposed to parent components when using ref. This hook should be used with forwardRef.
Signature
Parameters
Ref<T>
required
The ref that will be mutated. This is typically the ref forwarded from a parent component.
() => R
required
A function that returns the value to be attached to
ref.current. This should return an object with the methods/properties you want to expose.ReadonlyArray<unknown>
An array of dependencies. The effect will only activate if the values in this array change (compared using
===). If omitted, the handle is recreated on every render.Returns
void
Basic Usage
Video Player Control
Form Control
Modal Control
Canvas Drawing API
With Dependencies
useImperativeHandle should be used with forwardRef. It allows child components to expose a custom API to parent components through refs.This hook runs during the layout phase (like
useLayoutEffect), so the ref value is updated synchronously before the browser paints.Avoid overusing imperative APIs. Most interactions between components should be done declaratively through props. Use
useImperativeHandle sparingly for cases where you need to expose imperative methods like focus(), play(), or reset().