You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After updating from ~7 to latest (8.2.1), I noticed this curious behavior:
When using ref={(el) => ...} over a div element (my case), with the previous version I was able to use el.parentNode to traverse up the DOM tree and get refs to parents and so on.
With new version, el.parentNode is undefined, which makes me think the element gets generated but not yet inserted in the DOM tree? I can't figure out if this is a bug or if it's just a side-effect related to an improvement introduced in the last releases.
The text was updated successfully, but these errors were encountered:
cmaster11
changed the title
onRef gets now called before element exists in DOM (?)ref gets now called before element exists in DOM (?)
Jul 24, 2017
cmaster11
changed the title
ref gets now called before element exists in DOM (?)
ref={...} gets now called before element exists in DOM (?)
Jul 24, 2017
It was a change in 8, yes. I would recommend avoiding tree traversal from within a ref, it's not what refs are designed for. They're purpose is to allow you to store a reference and nothing more. If you want to traverse the tree, it's best to do so from a lifecycle event:
classFooextendsComponent{fooRef=c=>{this.foo=c;};componentDidMount(){// use the refletparentWidth=this.foo.parentNode.offsetWidth;}render(){return<divref={this.fooRef}/>}}
After updating from ~7 to latest (8.2.1), I noticed this curious behavior:
When using
ref={(el) => ...}
over adiv
element (my case), with the previous version I was able to useel.parentNode
to traverse up the DOM tree and get refs to parents and so on.With new version,
el.parentNode
is undefined, which makes me think the element gets generated but not yet inserted in the DOM tree? I can't figure out if this is a bug or if it's just a side-effect related to an improvement introduced in the last releases.The text was updated successfully, but these errors were encountered: