Window is not defined in Next.js React app

Leon Gaban picture Leon Gaban · Mar 13, 2019 · Viewed 71k times · Source

In my Next.js app I can't seem to access window:

Unhandled Rejection (ReferenceError): window is not defined

componentWillMount() {
    console.log('window.innerHeight', window.innerHeight);
}

Enter image description here

Answer

Darryl RN picture Darryl RN · Mar 16, 2019

Another solution is by using process.browser to just execute your command during rendering on the client side only.

if (process.browser) {
  // Client-side-only code
}