useImperativeHandle
useImperativeHandle — The Ref Customization Hook
Section titled “useImperativeHandle — The Ref Customization Hook”Introduction
Section titled “Introduction”useImperativeHandle customizes the value exposed to parent components when using forwardRef. Instead of exposing the entire DOM node, you expose only specific methods.
Why do we need this?
Section titled “Why do we need this?”forwardRef passes the ref through to a child’s DOM node. But sometimes you want to expose a custom API (focus, clear, validate) rather than the full DOM element.
Problem Statement
Section titled “Problem Statement”A custom <FancyInput> component should expose focus() and clear() but NOT allow direct DOM manipulation (like value). useImperativeHandle lets you define exactly what the parent can access.
Syntax
Section titled “Syntax”const Child = forwardRef((props, ref) => { const inputRef = useRef(null); useImperativeHandle(ref, () => ({ focus: () => inputRef.current.focus(), clear: () => { inputRef.current.value = ''; } })); return <input ref={inputRef} />;});Basic Example
Section titled “Basic Example”import { forwardRef, useRef, useImperativeHandle } from 'react';
const FancyInput = forwardRef((props, ref) => { const inputRef = useRef(null); useImperativeHandle(ref, () => ({ focus: () => inputRef.current.focus(), clear: () => { inputRef.current.value = ''; }, getValue: () => inputRef.current.value })); return <input ref={inputRef} {...props} style={{ padding: '8px', border: '2px solid #4f46e5' }} />;});
export default function Form() { const ref = useRef(null); return ( <div> <FancyInput ref={ref} placeholder="Type here..." /> <button onClick={() => ref.current.focus()}>Focus</button> <button onClick={() => ref.current.clear()}>Clear</button> </div> );}Best Practices
Section titled “Best Practices”💡 Only expose what’s necessary — hide internal implementation details.
🚀 Use for form inputs, modals, custom components with imperative APIs.
- What does useImperativeHandle do? A) Creates refs B) Customizes exposed ref value C) Forward refs D) Creates handles → B
- What hook must it be used with? A) forwardRef B) useState C) useEffect D) useRef → A
Summary
Section titled “Summary”🧠 useImperativeHandle = Custom Door — decide what visitors see and can touch.
📖 Customizes the ref value exposed to parents. Use with forwardRef.