Skip to content

How to Fix React ForwardRef Not Working

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to use forwardRef to pass refs through custom components. This matters because parent components cannot access child DOM nodes without forwardRef. In real-world applications, this pattern appears in form libraries needing focus, animation libraries needing DOM access.

The Wrong Way

function FancyInput(props) { return <input {...props} />; }
function Form() {
  const ref = useRef(null);
  useEffect(() => { ref.current.focus(); }, []);
  return <FancyInput ref={ref} />;
}
Warning: Function components cannot be given refs. ref.current null.

The Right Way

const FancyInput = React.forwardRef((props, ref) => <input ref={ref} {...props} />);
function Form() {
  const ref = useRef(null);
  useEffect(() => { ref.current.focus(); }, []);
  return <FancyInput ref={ref} />;
}
Input focused on mount. No warning.

Prevention

  • Wrap with forwardRef to expose DOM nodes
  • Pass forwarded ref to underlying DOM element
  • Use useImperativeHandle for custom APIs

FAQ

### Can I forward multiple refs??

forwardRef only forwards one ref. Pass refs as regular props or use useImperativeHandle to expose multiple refs in a single object.

Does forwardRef work with memo??

Yes. Wrap with forwardRef first, then pass to React.memo. Both preserve ref forwarding behavior.

What is the difference between forwardRef and callback refs??

forwardRef is a declarative API to receive and forward refs. Callback refs use a function as the ref prop. forwardRef uses callback refs internally.

This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro