react-sticky: Cannot read property 'getBoundingClientRect' of undefined
I’m not sure how to reproduce it, but we just got a report for the above error on the line:
https://github.com/captivationsoftware/react-sticky/blob/master/src/sticky.js#L69
return this.refs.placeholder.getBoundingClientRect().top;
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 35 (14 by maintainers)
No longer relevant with 6.x
I know this is an old issue but I was wondering if anyone actually got this to work? We’re using
react@16.2.0and I just can’t get it to work, even if I set everything up just like the examples. The<Stick />just stays where it is and theTypeError: Cannot read property 'getBoundingClientRect' of undefinederror pops up for every invocation.Any help is much appreciated 😃
Edit: I also get this:
I see a few problems off the bat @gregoralbrecht, here’s a working sandbox https://codesandbox.io/s/9op7mw8v4y
One of the problems is what the warning is telling you.
Stateless function components cannot be given refs, so whenStickytries to get a ref of the rendered component to measure, it ends up withundefined, ultimately giving you the error on this issue. You can fix that by using a class component.Another problem is that you’re not taking in the
styleargument from theStickycallback, which is the mechanism used to actually make the component stay where it is. The callback needs to be({ style }) =>at the bare minimum to work, and your ActionBar component needs to pass thestyleprop to the actual DOM.I’ve also just run into the same issue, using the most simple Sticky example. I’ve created a dedicated branch for anyone who wants to dive in at https://github.com/Vadorequest/chatounerie-du-luberon/tree/sticky-issue
Run
npm installthennpm startIncriminated file is https://github.com/Vadorequest/chatounerie-du-luberon/blob/sticky-issue/src/components/Navbar.js#L26
Hope it helps.
I had the same issue. I’m not sure why, but it got solved when I wrapped inside of my Sticky element with a div tag. I turned this:
<Sticky>{({style}) => <Image style={style} width="256" height="256" src="/assets/icon/windows.svg" alt="Windows"/> }</Sticky>To this:<Sticky>{({style}) => <div style={style}> <Image width="256" height="256" src="/assets/icon/windows.svg" alt="Windows"/> </div> }</Sticky>I didn’t get the
Stateless function components cannot be given refswarning, but it was the problem in my case. Thanks @vcarl.I was able to
git bisectand find where this issue was introduced. It turns out that for us it was caused by the New Relic Browser agent. Based on your explanation of what would cause this error to be thrown, I might guess that the NR Browser agents futzes with the DOM elements that the components hold a ref to or the events on window.Removing NR Browser immediately cleared the issue, so anyone else experiencing this issue should try the same.
@SpainTrain The source isn’t too bad; I recommend taking a look!
Having said that, the places that call
getBoundingClientRect()are all directly or indirectly called bySticky.recomputeState. That method is called whenever the props are updated (viacomponentWillReceiveProps) and via event handlers onwindow(which are attached incomponentDidMountand unattached incomponentWillUnmount).The things that would me most useful in diagnosing this error would be a) evidence that
getBoundingClientRectis being called from outside one of those two source (which would be an easy fix!), or b) a scenario whererecomputeStateis being called after the component has unmounted.