preventing rerenders on change in windows height

  javascript, react-hooks, react-native, reactjs, web

based on this, i can get the height and width of the window

but on change of height or width of the window, it causes rerenders, losing all data that has been previously entered

function getWindowDimensions() {
  const { innerWidth: width, innerHeight: height } = window;
  return {
    width,
    height
  };
}

 function useWindowDimensions() {
  const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions());

  useEffect(() => {
    function handleResize() {
      setWindowDimensions(getWindowDimensions());
    }

    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, []);

  return windowDimensions;
}



const { height, width } = useWindowDimensions();

this causes text input to lose focus and lose any data that may have been entered there

Source: Ask Javascript Questions

LEAVE A COMMENT