close

[Solved] Unhandled Rejection (ReferenceError): window is not defined

Hello Guys, How are you all? Hope You all Are Fine. Today I am trying to use windows.innerHeight But I am facing following error: Unhandled Rejection (ReferenceError): window is not defined in Reactjs. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Unhandled Rejection (ReferenceError): window is not defined Error Occurs ?

I am trying to use windows.innerHeight But I am facing following error.

Unhandled Rejection (ReferenceError): window is not defined

How To Solve Unhandled Rejection (ReferenceError): window is not defined Error ?

  1. How To Solve Unhandled Rejection (ReferenceError): window is not defined Error ?

    To Solve Unhandled Rejection (ReferenceError): window is not defined Error Just Use componentWillMount with undefined condition Here is my Example: if (typeof window !== ‘undefined’) { console.log(‘window innerHeight is:’, window.innerHeight); } } Now, Your error must be solved.

  2. Unhandled Rejection (ReferenceError): window is not defined

    To Solve Unhandled Rejection (ReferenceError): window is not defined Error You Just Need to use componentDidMount and inside componentDidMount You can Use windows. Here is My Example: componentDidMount() { console.log(‘window innerHeight is:’, window.innerHeight); } Now, Your error should be solved.

Solution 1: Use componentDidMount

You Just Need to use componentDidMount and inside componentDidMount You can Use windows. Here is My Example.

componentDidMount() {
  console.log('window innerHeight is:', window.innerHeight);
}

Now, Your error should be solved.

Solution 2: Use componentWillMount

Just Use componentWillMount with undefined condition Here is my Example.

componentWillMount() {
    if (typeof window !== 'undefined') {
        console.log('window innerHeight is:', window.innerHeight);
    }
}

Now, Your error must be solved.

Solution 3: Use useEffect

You Can Use useEffect Just like below example.

  React.useEffect(() => {
    
    console.log("window.innerHeight", window.innerHeight); // You can Access window

  }, []);

Hope Your error should be solved.

Summary

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment