Reputation: 553
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
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