Submitting the form below will ensure a prompt response from us.
In React, components are designed to be reusable and declarative. However, there are situations where a parent component needs direct access to a child component’s DOM node—for example, to focus an input field, trigger an animation, or integrate with a third-party library. This is where React forwardRef becomes useful.
forwardRef allows a ref to be passed from a parent component to a child component, even when that child is a functional component. This capability helps developers maintain clean component abstractions while still enabling controlled access to underlying DOM elements.
forwardRef is a React API that enables a component to receive a ref from its parent and pass it down to one of its child elements. By default, refs do not work with functional components unless forwardRef is used.
In simple terms:
React encourages encapsulation, but some scenarios require controlled access to DOM nodes.
Without forwardRef, these use cases would require workarounds that break component reusability.
The forwardRef function wraps a component and provides access to the ref as the second argument.
const MyComponent = React.forwardRef((props, ref) => {
return ;
});
Here:
import React from "react";
const InputField = React.forwardRef((props, ref) => {
return ;
});
export default InputField;
import React, { useRef } from "react";
import InputField from "./InputField";
function App() {
const inputRef = useRef(null);
const focusInput = () => {
inputRef.current.focus();
};
return (
<>
</>
);
}
export default App;
This example shows how forwardRef enables the parent to control the input field inside a child component.
useRef is mainly used to create a reference within a component, often for accessing DOM elements or persisting values without causing re-renders. In contrast, forwardRef allows a parent component to pass its ref down to a child component, enabling direct access to the child’s DOM or instance.
| Feature | useRef | forwardRef |
|---|---|---|
| Purpose | Create a ref | Forward a ref |
| Used In | Parent component | Child component |
| Access DOM | Direct | Indirect via child |
useRef creates the reference, while forwardRef passes it through components.
Sometimes you don’t want to expose the entire DOM element. Instead, you want to expose specific methods. This is where useImperativeHandle is useful.
import React, { forwardRef, useImperativeHandle, useRef } from "react";
const CustomInput = forwardRef((props, ref) => {
const inputRef = useRef();
useImperativeHandle(ref, () => ({
focus: () => inputRef.current.focus(),
clear: () => (inputRef.current.value = "")
}));
return ;
});
export default CustomInput;
This approach improves encapsulation while still allowing controlled access.
Use forwardRef when:
Avoid using it when simple prop-based communication is sufficient.
Understanding these pitfalls helps maintain clean architecture.
Moon Technolabs empowers businesses to build scalable, high-performance React applications by leveraging advanced React patterns, modern hooks, and component-driven architecture. Our team focuses on clean code practices, reusable components, and optimized state management to ensure applications remain maintainable and future-ready as they grow.
We also specialize in implementing advanced React features such as forwardRef, Suspense, concurrent rendering, and performance optimization techniques. By aligning UI development with business goals, we help reduce load times, improve user experience, and enhance application responsiveness across devices.
With deep expertise in integrating React with backend systems, cloud platforms, and third-party APIs, Moon Technolabs delivers end-to-end React solutions. Our approach ensures seamless collaboration, faster delivery cycles, and robust applications that support long-term digital transformation.
Need help implementing React Forwardref or designing reusable component APIs? Our React experts help you build scalable, high-performance frontends.
React forwardRef is a powerful feature that bridges the gap between component abstraction and DOM control. When used correctly, it enables flexible, reusable, and maintainable components without breaking React’s design principles. By combining forwardRef with hooks like useImperativeHandle, developers can build advanced UI components that are both robust and elegant.
Submitting the form below will ensure a prompt response from us.