React TutorialLesson 43Free preview
React Forward Ref
This lesson is part of React · All levels
Summary
ForwardRef allows the component to forward the ref received by the user to the DOM element in it. This is useful when packaging components such as custom inputs need to expose ref so that the parent can call the focus. ForwardRef is also often combined with useImperativeHandle to display a custom API.
Key points
- ForwardRef packs the function component to accept the ref as the second argument
- Ref is passed to the DOM element through the ref attribute
- Suitable for input, textarea, and scroll container
- UseImperativeHandle limits the method that can be called from outside
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.