I'm facing an issue, which I don't even understand if it's because of ReactJS, or it's just JavaScript.
Basically, method document.elementsFromPoint()
does't work if a ResizeObserver
is observing an element of the DOM.
Here's a fiddle that reproduce the problem:
class App extends React.Component {
divRef;
resizeObserver;
constructor(props) {
super(props);
this.divRef = React.createRef();
this.resizeObserver = new ResizeObserver(entries => {
console.log("Div resized");
this.forceUpdate();
});
}
componentDidMount() {
console.log("|| DIDMOUNT CONSOLE || ");
console.log(document.elementsFromPoint(150,150));
this.resizeObserver.observe(this.divRef.current);
}
componentDidUpdate() {
console.log("|| DIDUPDATE CONSOLE || ");
console.log(document.elementsFromPoint(150,150));
}
componentWillUnmount() {
this.resizeObserver.unobserve(this.divRef.current);
}
render() {
return (
<div ref={this.divRef} className="home-div" />
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
@import url(https://fonts.googleapis.com/css?family=Montserrat);
body {
font-family: 'Montserrat', sans-serif;
}
.home-div {
background: blue;
width: 500px;
height: 200px;
resize: both;
overflow: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root'></div>
As you can see, the log in componentDidMount()
, which uses document.elementsFromPoint()
, returns a bunch of elements.
Instead, the log in componentDidUpdate()
, which again uses document.elementsFromPoint()
, returns just the html
element, not all the others (even if the point is exactly the same, (150, 150)
).
Now, I thought that somehow React was re-rendering the whole DOM, thus, what's was before is no longer in the DOM, but there is no way componentDidUpdate()
returns just the html
element: componentDidUpdate()
is called AFTER the render has finished, thus there no way there is only the html
element.
Is this a bug? Or am I missing something, somewhere?