Forwarding Refs (React 19)
RE · useRefsyntax
// React 19: ref is a regular prop
function CustomInput({ ref, ...props }) {
return <input ref={ref} {...props} />;
}
// Pre-React 19 (still works):
const CustomInput = forwardRef((props, ref) => {
return <input ref={ref} {...props} />;
});example
// React 19 style: ref is just a prop
function FancyInput({ ref, placeholder, className }) {
return (
<input
ref={ref}
placeholder={placeholder}
className={`fancy-input ${className}`}
/>
);
}
// Parent component
function SearchBar() {
const inputRef = useRef(null);
return (
<div>
<FancyInput ref={inputRef} placeholder="Search..." />
<button onClick={() => inputRef.current.focus()}>Focus</button>
</div>
);
}Note In React 19, ref is passed as a regular prop to function components. forwardRef is no longer necessary and is considered deprecated. Existing code using forwardRef still works but should be migrated over time.