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.

Ref gives the wrong element after the first render

See original GitHub issue

I have a component called AppPane which has this render method (simplified):

render({children}) {
    return (
      <div ref={el => {
          console.log(el)
          this.rootEl = el
        }}>
        {children}
      </div>
    )
  }

The first console.log correctly logs the div element. But subsequent renders log AppPane. I can still access this.rootEl.base to get access to the div, so I do have a workaround.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisdaviescommented, Mar 12, 2016

I’d never heard of webpackbin… It is awesome.

Here’s the repro. You can click anywhere on the screen to increment the counter. Open up the browser console and you’ll see the issue:

http://www.webpackbin.com/E1WAAp22x

1reaction
developitcommented, Mar 12, 2016

would be useful. Try webpackbin.com if you haven’t yet, it’s awesome. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ref gives the wrong element after the first render #87 - GitHub
I have a component called AppPane which has this render method (simplified): render({children}) { return ( { console.log(el) this.
Read more >
useRef value is undefined on initial render - Stack Overflow
It's because the ref is updated after render time because the DOM element doesn't exist before render time. stackoverflow.com/questions/22238320 ...
Read more >
Refs and the DOM - React
When a ref is passed to an element in render , a reference to the node becomes accessible at the current attribute of...
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 >
Initializing focus state in React: you might be doing it wrong
But the element is focused, so our initial hasFocus state of false is wrong! To simulate this, I've delayed the hydration of the...
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