Capítulo 85 de 116
forwardRef(render) is the legacy mechanism for letting a function component accept a ref from its parent and forward it to a specific inner DOM node or expose a custom handle — superseded in modern React by accepting ref as a plain component prop directly.
ref isn't passed through to a function component like other props — a parent trying <MyInput ref={someRef} /> on a plain function component wouldn't reach anything inside it without extra plumbing.const MyInput = forwardRef((props, ref) => { return <input ref={ref} {...props} />; }) — wraps the component definition, receiving ref as a second argument separate from props.ref as a normal prop (function MyInput({ ref, ...props })) without needing the forwardRef wrapper at all — the docs steer new code toward this simpler, wrapper-free approach.useImperativeHandle (Ch 56) the same way either approach does, when the component needs to expose a customized handle rather than the raw DOM node.forwardRef remains necessary for reading/maintaining code written before the plain-ref-prop capability existed, even though new code shouldn't reach for it.// Legacy:
const MyInput = forwardRef((props, ref) => <input ref={ref} {...props} />);
// Modern equivalent, no wrapper needed:
function MyInput({ ref, ...props }) {
return <input ref={ref} {...props} />;
}
ref as a normal destructured prop rather than wrapping the component in forwardRef.forwardRef remains important to recognize when reading existing/older React codebases.useImperativeHandle is still the tool for customizing exactly what the forwarded ref exposes.