DEV Community

Discussion on: Window not defined

Collapse
 
sfiquet profile image
Sylvie Fiquet

Something like that:

const [windowWidth, setWindowWidth] = useState(0)

// runs only once, on mount
useEffect(() => {
  if (window !== undefined){
    setWindowWidth(window.innerWidth)
    window.addEventListener('resize', handleWindowWidth)
  }
}, [])
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
jishnu02 profile image
Jishnu-02

Thank You.