React Forward Ref

React Forward Ref

Ref forwarding lets a component receive a ref and pass it to one of its child DOM elements. This is useful when a parent component needs direct access to a nested input, button, or custom control.

Without ref forwarding, a ref attached to a custom component usually points to the component instance or nothing useful for DOM work.

Using forwardRef

import { forwardRef } from 'react';

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

// You can now successfully pass a native DOM ref down to <CustomInput ref={inputRef} />

When To Use It

Common Mistakes

Why It Matters

Ref forwarding keeps reusable components flexible. Parent components can still control focus or measure layout without giving up component abstraction.