0

I am developing Full stack Application IN Reactjs and Firebase I authenticatied successfully using signInWithPopup method after that I want store the credendials into local state.

I used this.setState in ComponentDidMount but getting an error. I even used axios but failed.


state = {

    } 

componentWillMount() {
       auth.onAuthStateChanged(function (user) {
            if (user) {
                // User is signed in.
                var displayName = user.displayName;
                console.log(displayName);
                var email = user.email;
                console.log(email);
                var emailVerified = user.emailVerified;
                console.log(emailVerified);
                var photoURL = user.photoURL;
                var isAnonymous = user.isAnonymous;
                var uid = user.uid;
                var providerData = user.providerData;

                this.setState({
                    displayName:this.displayName
                })
               } else {
                window.location = "/signin";
            }
        })
    }

TypeError: this.setState is not a function

Salil Sharma
  • 514
  • 1
  • 5
  • 14
  • Firstly, you should show your whole code. Also, you shouldn't do this in `willMount`, it should be in `didMount`. Thirdly, if you use `function` you're not going to have access to the `this` of the class. – Colin Ricardo Apr 02 '19 at 18:27

1 Answers1

1

Use auth.onAuthStateChanged((user) => { (arrow functions). The this used inside arrow functions and normal functions are different.

Also do not use componentWillMount instead use componentDidMount for the firebase authentication. Read this SO answer.

illiteratewriter
  • 4,155
  • 1
  • 23
  • 44