React/Advanced/Lesson 21
forwardRef and useImperativeHandle
25 min·theory
This chapter
2/2
TypeScript
forwardRef and useImperativeHandle
💡 Why should you learn this?
🎯
This is an advanced technique that every library developer must know.
💼
It is essential when building uncontrolled components.
⚡
This is a concept that frequently appears in technical interviews at large companies.
🏢 실무에서는
When building a UI library, the parent component may need to directly control a child's input. For example, when imperative operations such as 'setting focus' or 'resetting a value' are required, you use forwardRef together with useImperativeHandle. This is an essential skill for library developers.
What is forwardRef?
forwardRef and useImperativeHandle
The problem: refs cannot be passed as props
By default, ref is a special prop and is not forwarded to child components.
forwardRef
Connects the ref passed by the parent to a DOM element or instance inside the child component.
useImperativeHandle
Used together with forwardRef to explicitly define the interface exposed to the parent.
Use cases
- ▸Controlling
focus()on a custom Input component - ▸Controlling scroll position
- ▸Form library integration (React Hook Form's
Controller) - ▸Building reusable component libraries
React 19 change
Starting with React 19, you can receive ref as a regular prop without forwardRef.
💡 Practical tips
- ▸displayName: Set
displayNameon aforwardRefcomponent to show its name in React DevTools - ▸useImperativeHandle recommended: Expose only the methods you need instead of the entire DOM, for better encapsulation
- ▸React 19: ref can be passed as a regular prop;
forwardRefis no longer needed (backward compatibility maintained) - ▸React Hook Form:
forwardRefis used internally inside theControllercomponent - ▸TypeScript: Use the
forwardRef<RefType, PropsType>generic for type-safe usage
⚛️ React Pattern — forwardRef and useImperativeHandle
Learn step by step, with code, how forwardRef and useImperativeHandle are used in React.
1
🧩
1. Scenarios where forwardRef and useImperativeHandle are needed
Situations where these features are required.
↓
2
💻
2. Writing the code
Basic usage of forwardRef and useImperativeHandle.
↓
3
🎨
3. Rendering result
What the user sees on screen.
↓
4
💡
4. Real-world tips
Common pitfalls and best practices.
🎮 forwardRef and useImperativeHandle — Step-by-step Understanding
Click each step to read the content, then use the 'Got it' button to track your progress.
🖥️ Result — rendered React component
✏️ React 코드 수정하기 (클릭해서 열기)
⚛️ React 18 + Babel Standalone — see the result first, then edit the code yourself.
Check your understanding
When do you need forwardRef?
Read this first: Error Boundary + Suspense
Up next: TypeScript + React