Skip to content

forwardRef

In one line

forwardRef passes a ref through a component to its child — needed for ref forwarding in function components.

In simple words

forwardRef wraps a component to let the parent pass a ref through it to a child. Function components don't accept ref as a prop by default — forwardRef enables it.

Use cases:

  1. Forward to a DOM element — let parent focus a child's input
  2. Forward to another component — chain ref forwarding
  3. Combine with useImperativeHandle — expose custom methods instead of DOM node

Note: React 19 deprecates forwardRef — refs will be available as regular props. But for React 18 and earlier, forwardRef is required.

Example

jsx
import { forwardRef, useRef } from "react";

// Without forwardRef, ref doesn't work on custom components
const MyInput = forwardRef((props, ref) => (
  <input ref={ref} className="my-input" {...props} />
));

function Form() {
  const inputRef = useRef();
  return (
    <>
      <MyInput ref={inputRef} placeholder="Type here" />
      <button onClick={() => inputRef.current.focus()}>
        Focus the input
      </button>
    </>
  );
}

forwardRef lets the parent's ref point to the inner <input>. Without it, ref wouldn't reach the DOM element through the custom component.

Common confusions

  • Confused with: useRef

    The difference: useRef creates a ref object. forwardRef is a wrapper that lets a component accept a ref prop and pass it to a child. You use useRef to create the ref, and forwardRef to let it pass through a component boundary.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary