forwardRef Forwarding Refs

Last updated: 2026-11-02

forwardRef Forwarding Refs

Function components do not receive a ref by default. forwardRef forwards the ref passed in by the parent to a DOM node inside the component, so wrapped components such as a custom input or a button can be focused and measured just like a native element. React 19 accepts a ref prop directly, but older versions need forwardRef. The ref has to end up on a real DOM element, because forwarding it to an ordinary component that does not forward again leaves the parent with nothing.

GroupEffects & Performance
KindAPI
SinceReact 16.3

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

const FancyInput = forwardRef(function FancyInput(props, ref) {
  return <input ref={ref} {...props} />   // attach it to the real DOM
})

// usage: <FancyInput ref={inputRef} />

⚙️ Parameters

render functionA function of the shape (props, ref) => JSX. The second argument is the ref passed by the parent, and it must be attached to a real DOM node or to a component that forwards it further.

Returns:Returns a new component that can forward a ref: ref.current in the parent will point at the node the render function attached.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

After the click the blue-bordered input is focused and its content is cleared — the ref passed through the FancyInput wrapper and reached the real input inside.

💡 Key Points

💡 Tip In React 19 a function component can receive a ref prop directly, so forwardRef is no longer needed; older versions require it.
ℹ️ Info The ref argument must be attached to a real DOM element; attaching it to an ordinary component that does not forward means the parent receives nothing.
⚠️ Warning Do not abuse forwarded refs to poke inside a child component — prefer declarative props, and combine useImperativeHandle with it when you need a richer imperative interface.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Why can a function component not receive a ref directly?
A A function component has no instance for a ref to attach to; React 18 and earlier kept the ref out of props, so forwardRef had to forward it explicitly.
Q What changed in React 19?
A The ref became an ordinary prop, so a function component can simply write function Cmp({ ref }), and forwardRef is no longer required (though it still works).
Q How is useImperativeHandle different from forwarding directly?
A Forwarding directly exposes the whole DOM node; useImperativeHandle lets you define the exposed set of methods (focus, clear, …) and hide the implementation.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏