Hook for forwardRef
See original GitHub issueDo you want to request a feature or report a bug? feature What is the current behavior? Today we use forwardRef as a HOC around functional component, like this:
const MyComponent = React.forwardRef((props, ref) => {
return (<div {...props} ref={ref}>Some text</div>);
});
MyComponent.displayName = 'MyComponent';
What is the expected behavior? Is it possible to use hooks to forward ref? For example, something like this:
const MyComponent = (props) => {
// will return ref that was passed from parent component
const forwardedRef = useForwardredRef();
return (<div {...props} ref={forwardedRef}>Some text</div>);
}
With this approach we don’t need to manually set displayName all the time, also this would be great for libraries, as you still export same component, not a forwardRef HOC.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React 16.8 and upper
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Using forwardRef with React hooks | by JB
The forwardRef hooks allows React users to pass refs to child components. The ref can be created and referenced with useRef or createRef...
Read more >forwardRef Hook - React Hooks Handbook - Design+Code
As the name implies, forwardRef is used to forward a ref to a child component, when we wish to access that ref further...
Read more >Understanding the use of useRef hook & forwardRef in ...
The useRef hook in react is used to create a reference to an HTML element. Most widely used scenario is when we have...
Read more >Forwarding Refs - React
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words,...
Read more >reactjs - forwardRef with custom component and custom hook
One issue that I'm seeing is that in the Input component, you're using props.input , why? const Input = forwardRef((props, ref) => {...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The plan is to eventually get rid of the need for
forwardRef
altogether by putting it back intoprops
.https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md#move-ref-extraction-to-class-render-time-and-forwardref-render-time
We’re not quite there yet.
I guess it is somewhat non-compositional because you can’t pass the same ref to two things. Although it’s not as bad as some other non-compositional Hooks.
Any updates?