React Course
React
/
Advanced

useImperativeHandle

Definition

`useImperativeHandle` customizes the instance value that is exposed to parent components when using `ref`. It allows a child component to explicitly define which properties or methods a parent component can call on it.

Explain Like I'm New

Normally in React, a parent controls a child using Props (Declarative). But sometimes a parent needs a remote control to force the child to do something specific (Imperative). `useImperativeHandle` is the child building that remote control, putting specific buttons on it (like 'focusInput' or 'resetForm'), and handing the remote up to the parent.

Real World Example

You build a `<CustomVideoPlayer />` component. The parent component wants a button to play or pause the video. You use `forwardRef` and `useImperativeHandle` inside the CustomVideoPlayer to expose a `.play()` and `.pause()` method to the parent.

Common Use Cases

  • Exposing specific DOM methods (like focus(), scrollIntoView()) to parents
  • Triggering complex animations or state resets inside a child from a parent
  • Integrating React with imperative libraries (like D3 or jQuery)

Interactive Example

import React, { useRef, useImperativeHandle, forwardRef } from 'react';

// 1. The Child Component (MUST be wrapped in forwardRef)
const CustomInput = forwardRef((props, ref) => {
  const inputRef = useRef(null);

  // 2. Define the exact 'remote control' we hand to the parent
  useImperativeHandle(ref, () => ({
    // Expose a custom focus method
    focusAndClear: () => {
      inputRef.current.value = '';
      inputRef.current.focus();
    },
    // Expose a custom shake method
    shake: () => {
      console.log("Shaking the input via CSS animation!");
    }
  }));

  return <input ref={inputRef} type="text" placeholder="Type here..." />;
});

// 3. The Parent Component
export default function App() {
  const childRef = useRef(null);

  return (
    <div>
      <CustomInput ref={childRef} />
      <button onClick={() => childRef.current.focusAndClear()}>
        Clear & Focus Input
      </button>
      <button onClick={() => childRef.current.shake()}>
        Shake Input
      </button>
    </div>
  );
}

Interview Questions

basic

  • What React utility MUST wrap a component before it can use `useImperativeHandle`?
  • Why is imperative programming discouraged in React?

intermediate

  • How does `useImperativeHandle` relate to `forwardRef`?
  • What arguments does `useImperativeHandle` take?

advanced

  • Can you expose internal state variables of a child component to a parent using this hook?
  • What is the third (optional) argument of `useImperativeHandle`?

trick

  • If a parent calls an imperative method on a child, does the child automatically re-render?

Flash Cards

Question

What utility must wrap the component?

Click to reveal answer
Answer

The component MUST be wrapped in `React.forwardRef()`. This allows the child component to receive the `ref` passed down from the parent.

Question

Why is imperative programming discouraged?

Click to reveal answer
Answer

React is designed to be Declarative (UI is a direct function of State). Imperative code (telling the code EXACTLY what to do step-by-step) breaks this paradigm, making code harder to track, test, and debug. It should only be used as an escape hatch.

Question

Does calling an imperative method automatically trigger a re-render?

Click to reveal answer
Answer

No. Unless the imperative method explicitly calls a `setState` function inside the child, simply calling the method does not cause a re-render.