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.

How to get ref to DOM element of Menus/Popovers

See original GitHub issue

I’m trying to make material-ui-popup-state’s bindHover keep the popover open while either the anchor element or the menu or popover is hovered (https://github.com/jcoreio/material-ui-popup-state/issues/3). I can’t do this properly unless I can get a ref to the actual DOM element of the menu or popover.

But with findDOMNode being deprecated, it seems like I’m screwed.

Can’t use PaperProps={{ref: c => this.popover = c}}, that gets a reference to a component instead of a DOM element. Paper doesn’t support any kind of innerRef prop; I could pass it a component that renders with the ref I need but that would be messy.

To make matters worse, if I find the DOM element with the id I pass to the menu or popover, it’s the container element that fills the whole screen rather than the menu/popover part. Same goes for the currentTarget of event handlers passed to menu or popover.

Maybe I can use PaperProps={{ onMouseEnter: e => this.popover = e.currentTarget }}, but the problem with that is the mouseenter event would come after the mouseleave on the anchor element.

This sucks. I kind of hate that the React team doesn’t want to allow us to find the root DOM node within a Component.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
oliviertassinaricommented, Mar 13, 2019

I’m closing for #10825. It should be good now after @eps1lon work:

      <Popover
        ref={ref => {
          console.log('ref', ref);
        }}
        id={id}

Outputs in the console:

Capture d’écran 2019-03-13 à 17 59 44

1reaction
rotimi-bestcommented, Apr 19, 2021

@oliviertassinari oh that’s cool, I tried googling it but couldn’t find it. How do I know when a ref resolves in a function (like in a useEffect or a prop function)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get ref to DOM element of Menus/Popovers #14342
I'm trying to make material-ui-popup-state's bindHover keep the popover open while either the anchor element or the menu or popover is ...
Read more >
Refs and the DOM - React
Ref forwarding lets components opt into exposing any child component's ref as their own. You can find a detailed example of how to...
Read more >
Get Ref element from DOM - Stack Overflow
How can I get the ref on the required element, that is, I have an array of ... nodes = elementsWithScrolls() // Array...
Read more >
How to Access DOM Nodes in React with Refs - Fullstack.io
In this article you'll learn how to access underlying DOM elements in React. This can be useful in scenarios when there is no...
Read more >
The Complete Guide to useRef() and Refs in React
How to use React.useRef() hook to create persisted mutable values (also known as references or refs), as well access DOM elements.
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