Salil Sharma
Salil Sharma

Reputation: 553

After authentication from the firebase i want to store Name, Email in state

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

Upvotes: 0

Views: 63

Answers (1)

illiteratewriter
illiteratewriter

Reputation: 4333

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.

Upvotes: 1

Related Questions