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.

`Handle` does not forward `ref` prop to contained `<div>`

See original GitHub issue

We’re building a flow programming tool where nodes can have multiple inputs and outputs.

Currently these handles are being rendered with a piece of text as a child, and this text is an unknown length.

As such, the Handle is being styled as such:

style={{
  top: `${top}px`,
  height: '15px',
  width: 'auto',
  left: `-${width}px`
}}

Note: Additional styling is applied but these are the important ones as far as positioning go.

I have width defined as state in a wrapping component, I need to be able to get the width of the inner div rendered by the Handle component, but passing <Handle ref={handleRef} /> never resolves.

It would be useful if Handle forwarded the ref prop as detailed here Forwarding refs to DOM components

I can see here: https://github.com/wbkd/react-flow/blob/64d1913356d171fd2c4303c918c3e375f57cbee7/src/components/Handle/index.tsx#L95

The ...rest of the props are being applied to the div, but I’ve got a feeling that it doesn’t work for ref and instead it needs explicitly forwarding using React.forwardRef

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ckpearsoncommented, Aug 20, 2021

It’s perhaps worth noting that I’ve been able to sidestep it by:

  1. Making the handle a flex container
  2. Adding a child div with flex-1 and width:100%
  3. Connecting that div with the ref
0reactions
moklickcommented, Sep 1, 2021

We are now forwarding the ref. Released in v9.6.7

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forwarding Refs - React
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >
Using forwardRef in React to clean up the DOM
In this tutorial, we will go over the concept of forwarding refs in React and understand how it helps us manage interactions with...
Read more >
How to use React's forwardRef function | Felix Gerschau
Our task is to forward the ref that the Input component receives to the HTML input element. We do that by wrapping the...
Read more >
How do I avoid 'Function components cannot be given refs ...
Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Check the render method of ForwardRef . in NavLink (created...
Read more >
Using refs in React functional components (part 2)
The ref type passed to the forwardRef function is not a HTMLDivElement because with the useImperativeHandle we are creating a new return to...
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