Back to React Notes
Topic #43

React Forward Ref

forwardRef passes a ref from a parent into a child component, useful for focusing inputs and wrapping UI libraries. It is a key piece of component interop.


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 Coding Now Tech Institute'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 Coding Now Tech Institute 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 Coding Now Tech Institute, 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 Coding Now Tech Institute'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 Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now