Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The component-based architecture of React allows us to have a hierarchical structure of components with parent and child components.
In the typical React dataflow, props are the only way that parent components interact with their children.
To modify a child, you re-render it with new props.
However, there are a few cases where you need to change a child outside of the typical data flow imperatively.
The child to be modified could be an instance of a React component or a DOM element. For both of these cases, React provides an escape hatch to this with the help of refs.
So basically, Refs provide a way to access DOM nodes or React elements created in the render method.
We are going to learn about forwarding refs. Let us see ahead what are forwarding refs in react.
For a brief understanding, Ref forwarding is a technique for automatically passing a ref through a component to one of its children.
Forwarding refs is typically not necessary for most components in the application. However, it can be helpful for some kinds of components, especially in reusable component libraries.
This concept can be best understood with an example so let's dive right into the code and then go over how it works.
How to Implement forwarding refs?
Consider an application where what we want to achieve is when we click on the button, and the parent component, the input in the child component should receive focus. However, we will use the forwarding ref technique to allow the parent component to reference the native input element directly.
Before forwarding refs
For this example, To reach the stage as shown in the above image, we will have to create two files—namely, FRInput.js and FRParentInput.js in the components folder.
FRInput.js
This is a functional component that simply returns an input field.
import React from "react";
function FRInput() {
return (
<div>
<input type="text" />
</div>
);
}
export default FRInput;
You can also try this code with Online Javascript Compiler
Step3: we need to forward this ref to the input element in the child component, and ref forwarding can be achieved using the forwardRef method from the react library.
FRInput.js
In FRInput.js, which is our child component, we will modify how we create the functional component.
The new way to define the functional component is to simply replace the traditional function with an arrow function. You can see that we have not changed any functionality. We have simply rewritten the functional component using arrow functions.
For forwarding refs, we use the default forwardRef method, which takes in a component as a parameter, so we simply place the arrow function into the argument of this method.
We know that every functional component receives props as its parameter but let me tell you, when a component is passed as a parameter to the create ref method, it gets the ref attribute as its second parameter. We can use this ref parameter and pass it to the ref attribute on the native input element.
This ref parameter will point to the value of the ref attribute from the parent component, or in other words, the ref is being forwarded from the parent component to the native input element.
The App.js file will stay the same as above. After doing all the changes, we see that in the browser, when we click on the “Focus input” button, the input field is getting focussed with the help of the forwarding refs technique.
Output
Other uses of forwarding refs
Some of the application based uses of forwarding refs are:
Ref forwarding is a technique for automatically passing a ref through a component to one of its children.
Which method is used for forwarding refs in react?
We use forwardRef method in react to implement the forwarding refs.
What are the parameters of the component that creates the forwardRef method?
The functional component that creates the forwardRef method has two parameters, namely, props and ref.
Key Takeaways
We learned about the concept of forwarding refs in react. This is an advanced concept of forwarding refs in React that allows us to automatically pass a ref through a component to any of its children. This article also explained what are the uses for forwarding refs in React specifically. We also learned about the implementation of forwarding refs in React.
Apart from this, you can also expand your knowledge by referring to these articles on Javascript and React.
For more information about the react framework for frontend development, get into the entire Frontend web development course.
Live masterclass
Get shortlisted for Amazon data interview: SQL+Python Prep
by Abhishek Soni
26 Jun, 2025
01:30 PM
Prompt Engineering: New Must-Have GenAI Skill for Developers
by Anubhav Sinha
23 Jun, 2025
01:30 PM
Using Netflix Data to Master Power BI
by Ashwin Goyal
24 Jun, 2025
01:30 PM
Build ChatGPT-like Search Using RAG – Live with Google SDE2
by Saurav Prateek
25 Jun, 2025
01:30 PM
Get shortlisted for Amazon data interview: SQL+Python Prep
by Abhishek Soni
26 Jun, 2025
01:30 PM
Prompt Engineering: New Must-Have GenAI Skill for Developers