Course contentsShow
← React
Lesson 724 of 2,39619. useRefFree lesson

The Ref Forwarding Problem

Why you can't pass ref as a regular prop and how React treats ref specially on built-in elements.

The Ref Forwarding Problem

What you'll learn: How to pass ref to your custom components, and how React 19 solved the historical "ref forwarding" problem.


You've learned that you can attach a ref to built-in HTML elements like <input> or <div> to access their DOM nodes directly. But what happens when you try to pass a ref to your own custom component, like a <TextInput>?

The Modern Solution: ref as a Prop

In modern React (React 19 and later), passing a ref to a custom component is straightforward. Function components receive ref as a regular prop, just like className or onChange.

If you want your custom <TextInput> component to expose its inner <input> element to the parent component, you simply accept the ref prop and pass it down to the built-in element:

// React 19+ approach
function TextInput({ ref, placeholder }) {
  // Pass the ref prop down to the built-in <input>
  return <input ref={ref} placeholder={placeholder} />;
}

// In the parent component:
function App() {
  const inputRef = useRef(null);
  
  // Passing the ref works seamlessly
  return <TextInput ref={inputRef} placeholder="Type here..." />;
}

The Historical "Ref Forwarding" Problem

If you work with older codebases (React 18 and earlier), you will frequently hear about the "ref forwarding problem."

Historically, React treated ref as a reserved keyword. When you wrote <TextInput ref={myRef} />, React intercepted the ref. Because React didn't know which internal element of your custom component the ref should actually attach to (an outer <div>? an inner <input>?), it deliberately stripped ref out of the props. If you tried to access props.ref inside the component, it would be undefined.

To solve this in older versions, developers had to explicitly "forward" the ref using a special API called forwardRef:

// Legacy pre-React 19 approach
import { forwardRef } from 'react';

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

Today, forwardRef is a legacy API kept only for backwards compatibility. You do not need it when writing modern React code.

Key Takeaway: In React 19 and later, function components receive ref as a regular prop, making it incredibly easy to pass refs down. The historical "ref forwarding problem"—where ref was stripped from props and required the forwardRef API—is a legacy pattern you will only see in older codebases.