📌  相关文章
📜  窗口调整大小反应 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:09.892000             🧑  作者: Mango

代码示例1
import React, { useLayoutEffect, useState } from 'react';

function useWindowSize() {
  const [size, setSize] = useState([0, 0]);
  useLayoutEffect(() => {
    function updateSize() {
      setSize([window.innerWidth, window.innerHeight]);
    }
    window.addEventListener('resize', updateSize);
    updateSize();
    return () => window.removeEventListener('resize', updateSize);
  }, []);
  return size;
}

function ShowWindowDimensions(props) {
  const [width, height] = useWindowSize();
  return Window size: {width} x {height};
}