DEV Community


Discussion on: Make Your React Apps Responsive with a Custom Hook

alexpravdinspringernature profile image
Alex Pravdin • Edited

How does this work?

const isSSR = typeof window !== "undefined";
  const [windowSize, setWindowSize] = React.useState({
    width: isSSR ? 1200 : window.innerWidth,
    height: isSSR ? 800 : window.innerHeight,
Enter fullscreen mode Exit fullscreen mode

If the window is undefined, then isSSR = false, then we read property value from undefined in the following line isSSR ? 1200 : window.innerWidth? Is it a mistype in the article or I understand something wrong? Why does this code assume that it's on the server when the window variable is present (not equal to the undefined) and we can read window dimensions from it?

Forem Open with the Forem app