Details

Demo using React hooks to listen for window size changes.

Online demo: https://react-1tcrpt.stackblitz.io/

Code

import React, { useEffect, useState } from "react"
import "./style.css"

export default function App() {

  const [size, setSize] = useState({w: window.innerWidth, h:window.innerHeight})

  useEffect(() => {
    const handleResize = () => {
      setSize([window.innerWidth, window.innerHeight])
    }
    window.addEventListener('resize', handleResize)
    return () => window.removeEventListener('resize', handleResize)
  }, [])

  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Window size: {size.w} x {size.h}px</p>
    </div>
  )
}