📜  副作用,useEffect,返回 - Javascript 代码示例

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

代码示例1
function WindowSizeList({ url }) {
  const [windowWidth, setWindowWidth] = useState(window.innerWidth)
  const [items, setItems] = useState([])

  const updateWindowWidth = () => {
    setWindowWidth(window.innerWidth)
  }

  useEffect(() => {
    setItems(CustomApi.getList(url))
  }, [url])

  useEffect(() => {
    window.addEventListener('resize', updateWindowWidth)
    return () => {
      window.removeEventListener('resize', updateWindowWidth)
    }
  }, [])

  return (
    <>
      
Window Width: {windowWidth}
{items.map(item => { return
{item}
})} ) }