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.

use ref instead of identifier + document.getElementById()

See original GitHub issue

I’m using your library for inspiration for a project where I have to wrap a vanillaJS wrapper of vis. So thank you!

In the process of doing that, I discovered that there is a more idiomatic way to get the underlying HTML node to pass to vis. You are using getElementById here: https://github.com/crubier/react-graph-vis/blob/master/src/index.js#L86

Instead, you can create a ref in your constructor:

this.container = React.createRef();

Attach it to your <div> in the render() method: <div ref={this.container} />

Then grab the reference to it here: https://github.com/crubier/react-graph-vis/blob/master/src/index.js#L109

this.container.current

In particular, this helps when running tests with Enzyme. I’m not sure where else it might help. If you want a PR, let me know, but those are the basic changes, I think.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
crubiercommented, Feb 15, 2021

Yes this is now fixed, thanks!

1reaction
crubiercommented, Aug 29, 2018

True, this is old code that needs to be updated. I will try to have a look when I have more time, but if anyone wants to create a PR, feel free to do so, it’s just a few lines of code 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs this.refs vs document.getElementById - Stack Overflow
In general, refs is better than document.getElementById , because it is more in line with the rest of your react code. In react,...
Read more >
What is the equivalent of document.getElementById in React
The equivalent of the document.getElementById() method in React is using refs. To select an element, set the ref prop on it to the...
Read more >
Why to use refs instead of IDs - JavaScript Stuff
Let's start by assuming that you went ahead and used an ID to access one of your elements in a React component. What...
Read more >
Why to use refs instead of IDs In React JS - SKPTRICKS
Lets start with ID attribute in react js and here we have used ID attribute to access one of the element in react...
Read more >
Reason behind using refs instead of ID in React JS
NOTE: id attribute works on a single element in whole DOM tree. To overcome this animation issue, always use Ref instead of id's....
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