React js document event "readystatechange" not called

Viewed 13

I recently changed my pc I was working with, uploaded all my code on github and cloned it back and something strange is happening, the readystatechange event of the document is not getting fired anymore.

I use React as my framework and I have this code that wait for the document to be ready to find the dimension in pixels of a flexbox div.

function useElementDimension(elementId: string) {
  const [dimensions, setDimensions] = useState({
      height: 0,
      width: 0
  });

  useEffect(() => {
    function onLoaded() {
      if(document.readyState != "complete") return;
      const stage = document.getElementById(elementId);
      if(stage === null) return;
      setDimensions({width: stage.clientWidth, height: stage.clientHeight});
    }

    document.addEventListener('readystatechange', onLoaded);
    return (() => document.removeEventListener('readystatechange', onLoaded));
  });

  return dimensions;
}

It was working just fine in my other laptop, the effect is getting called and registering the event but the function onLoaded is never called. Any idea?

0 Answers
Related