question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hook for forwardRef

See original GitHub issue

Do 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:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
gaearoncommented, Apr 3, 2019

The plan is to eventually get rid of the need for forwardRef altogether by putting it back into props.

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.

Adding a useForwardRef hook would probably break https://overreacted.io/why-isnt-x-a-hook/.

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.

2reactions
Peters8090commented, Aug 29, 2020

Any updates?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found