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
This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
← Previous
How to Fix React PropTypes Validation Errors
Next →
How to Fix React Render Props Pattern
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro