![]() |
VOOZH | about |
The useImperativeHandle hook in react is used to modify the instance of child component while using refs. It is an additional hook that works with refs and allows us to customize the instance values of Child Components without directly using DOM.
useImperativeHandle(ref, createHandle, [deps])Parameters:
Returns:
It gives access to customize the instances.
The useImperativeHandle hook works in the similar phase of useRef hook but only it allows us to modify the instance that is going to be passed with the ref object which provides a reference to any DOM element. Although this hook is used in rare cases, it has some most advanced functionality.
Let's understand it with an example, but first, create a new react application.
Example: This example demonstrates how to use useImperativeHandle to expose the focus method from a child to its parent allowing the parent to focus the input field.
Step to Run Application: Run the application using the following command from the root directory of the project:
npm startOutput:
👁 Image