🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to React Notes
Topic #43

React Forward Ref


What is forwardRef?

forwardRef lets your component pass a reference to one of its children. It's like giving a direct reference to a DOM element inside your component.

Common uses for forwardRef:

  • Focusing input elements
  • Triggering animations
  • Measuring DOM elements
  • Integrating with third-party libraries

Basic Example

Here's a simple example of forwarding a ref to an input element:

Example

import { forwardRef, useRef } from 'react';

const MyInput = forwardRef((props, ref) => (
  <input ref={ref} {...props} />
));

function App() {
  const inputRef = useRef();

  const focusInput = () => {
    inputRef.current.focus();
  };

  return (
    <div>
      <MyInput ref={inputRef} placeholder="Type here..." />
      <button onClick={focusInput}>Focus Input</button>
    </div>
  );
}

In this example:

  • We wrap our input component with forwardRef
  • The component receives a ref as its second parameter
  • The parent can now control the input element directly

Note: Only use forwardRef when you need direct access to a DOM element. For most cases, you can use props and state instead.

Want to go beyond the notes?

Join CodingNow 2.0's React course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

React Forward Ref – FAQs

Quick answers about learning React Forward Ref in React.

This free note from CodingNow 2.0 explains React Forward Ref in React — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every React topic on CodingNow 2.0, including React Forward Ref, is 100% free with no signup required.
With focused practice, most students grasp React Forward Ref in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now