react-toolbox: Tabs: "Cannot read property 'getBoundingClientRect' of null"
Problem
Reliably getting an error with HMR updates:Uncaught TypeError: Cannot read property 'getBoundingClientRect' of null at Tabs.js:86
Context
- Version:
2.0.0-beta.8
- Error is pointing to this line: https://github.com/react-toolbox/react-toolbox/blob/2.0.0-beta.8/components/tabs/Tabs.js#L85
- In the inspector
this.navigationNode
equalsnull
- THIS IS SUPER SURPRISING since it’s behind a guard clause:
if (this.navigationNode /* && ... */) { // ... // somehow this.navigationNode is null !!! object mutation, race condition? inside a callback? }
- THIS IS SUPER SURPRISING since it’s behind a guard clause:
Possibly related: On my team, we’re not using the component to render the content of each tab (props.children
of<Tab />
is undefined). We’re only using it for the navigation and we handle displaying tab content somewhere else. Not sure if this is totally relevant.
Possible Solutions
Pull this.navigationNode
into a variable.
From
updatePointer = (idx) => {
if (this.navigationNode && this.navigationNode.children[idx]) {
requestAnimationFrame(() => {
const nav = this.navigationNode.getBoundingClientRect();
const label = this.navigationNode.children[idx].getBoundingClientRect();
const scrollLeft = this.navigationNode.scrollLeft;
this.setState({
pointer: {
top: `${nav.height}px`,
left: `${(label.left + scrollLeft) - nav.left}px`,
width: `${label.width}px`,
},
});
});
}
TO
updatePointer = (idx) => {
const navigationNode = this.navigationNode
if (navigationNode && navigationNode.children[idx]) {
requestAnimationFrame(() => {
const nav = navigationNode.getBoundingClientRect();
const label = navigationNode.children[idx].getBoundingClientRect();
const scrollLeft = navigationNode.scrollLeft;
this.setState({
pointer: {
top: `${nav.height}px`,
left: `${(label.left + scrollLeft) - nav.left}px`,
width: `${label.width}px`,
},
});
});
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Fix Tabs null ref - References #1535 Pulling `this.navigationNode` out to a variable fixes the null reference problem, but reveals a new problem! Now we're getting an error about setting state on a... — committed to manuphatak/react-toolbox by manuphatak 7 years ago
- Add null-guards for lazily set navigationNode field of Tabs This will address https://github.com/react-toolbox/react-toolbox/issues/1535 and premptively fix a couple of other harder-to-hit race condi... — committed to aheitzmann/react-toolbox by deleted user 7 years ago
It should be reopened. still exists on
2.0.0-beta.12
yes, we have forked react toolbox and pulled that commit in, still seeing the same issues.
I think it’s not released @jodonnell . I’m releasing a new version that should cover the fix