【问题标题】:After authentication from the firebase i want to store Name, Email in state从 Firebase 进行身份验证后,我想在状态下存储名称、电子邮件
【发布时间】:2019-04-02 18:23:33
【问题描述】:

我正在开发 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";
            }
        })
    }

TypeError: this.setState 不是函数

【问题讨论】:

  • 首先,你应该展示你的整个代码。另外,你不应该在willMount 中这样做,它应该在didMount 中。第三,如果您使用function,您将无法访问班级的this

标签: javascript reactjs firebase-authentication axios state


【解决方案1】:

使用auth.onAuthStateChanged((user) => {(箭头函数)。箭头函数内部使用的this 与普通函数不同。

也不要使用componentWillMount 而是使用componentDidMount 进行firebase 身份验证。阅读this SO答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2018-02-10
    • 2018-09-22
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2020-11-22
    • 2019-05-20
    相关资源
    最近更新 更多