我正在 Reactjs 和 Firebase 中开发全栈应用程序,我使用signInWithPopup 方法成功进行了身份验证,之后我想将凭证存储到本地状态。
我在 ComponentDidMount 中使用了 this.setState 但出现错误。我什至使用了 axios 但失败了。
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";
}
})
}
类型错误:this.setState 不是一个函数
最佳答案
使用auth.onAuthStateChanged((user) => {
(箭头函数)。箭头函数和普通函数内部使用的this
是不同的。
也不要使用 componentWillMount
而是使用 componentDidMount
进行 Firebase 身份验证。阅读 this所以回答。
关于javascript - 从 firebase 进行身份验证后,我想在状态中存储名称、电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55481287/