useImperativeHandle in React

From the React Hooks cheat sheet · Additional Hooks · verified Nov 2026

useImperativeHandle

Customize ref values

javascript
// Child component - ref is a normal prop in React 19 (no forwardRef)
function Child({ ref }) {
  const inputRef = useRef(null)

  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current.focus(),
    clear: () => { inputRef.current.value = '' },
    getValue: () => inputRef.current.value
  }))

  return <input ref={inputRef} />
}

// Parent component
const childRef = useRef(null)
<Child ref={childRef} />
childRef.current.focus()               // e.g. in an event handler
💡 Expose imperative API to parent
⚡ React 19: ref is a normal prop - no forwardRef needed
⚠️ Use sparingly - breaks React patterns
📌 Optional 3rd arg: deps array - the handle is recreated when deps change

Continue with React Hooks

Save the full cheat sheet or work through every related task.

More React tasks

Back to the full React Hooks cheat sheet