反应firebase auth()setState不在then()内部工作

时间:2017-07-17 21:50:22

标签: javascript reactjs firebase firebase-realtime-database firebase-authentication

我从firebase文档中签了这个twitter,我想保存uid状态,但它不起作用。有什么想法在这里发生吗?

twitterSignin(provider) {

    var provider = new firebase.auth.TwitterAuthProvider();
    var getStoreID = this.props.storeId;

    firebase.auth().signInWithPopup(provider)
        .then(function (result) {
            var token = result.credential.accessToken;
            var user = result.user;

            var ref = firebase.database().ref(getStoreID);

            ref.once("value")
                .then(function (snapshot) {
                    var data = snapshot.val();

                    if (!data.owner) {
                        ref.set({
                            owner: result.user.uid
                        })
                    }

                    this.setState({
                        uid: result.user.uid
                    })

                });
        }).catch(function (error) {
            console.log(error.code)
            console.log(error.message)
        });
}

由于

修正:

    twitterSignin(provider) {

        var provider = new firebase.auth.TwitterAuthProvider();
        var getStoreID = this.props.storeId;
var that = this;

        firebase.auth().signInWithPopup(provider)
            .then(function (result) {
                var token = result.credential.accessToken;
                var user = result.user;

                var ref = firebase.database().ref(getStoreID);

                ref.once("value")
                    .then(function (snapshot) {
                        var data = snapshot.val();

                        if (!data.owner) {
                            ref.set({
                                owner: result.user.uid
                            })
                        }

                        that.setState({
                            uid: result.user.uid
                        })

                    });
            }).catch(function (error) {
                console.log(error.code)
                console.log(error.message)
            });
    }

1 个答案:

答案 0 :(得分:5)

问题在于您尝试从其他范围访问setState。为了解决问题,您需要将回调绑定到实例。为此,您有两个选项,使用bind,缓存父作用域const me = this;,然后在回调中使用me或使用箭头函数。

twitterSignin(provider) {

  var provider = new firebase.auth.TwitterAuthProvider();
  var getStoreID = this.props.storeId;

  firebase.auth().signInWithPopup(provider)
    .then((result) => {
      var token = result.credential.accessToken;
      var user = result.user;

      var ref = firebase.database().ref(getStoreID);

      ref.once("value")
        .then((snapshot) => {
          var data = snapshot.val();

          if (!data.owner) {
            ref.set({
             owner: result.user.uid
            })
          }

          this.setState({
            uid: result.user.uid
          })

        });
  }).catch(function (error) {
    console.log(error.code)
    console.log(error.message)
  });
}

我选择箭头功能。

箭头函数在外部范围内执行,在这种情况下是twitterSignin的范围,它是您的反应组件,setState的那个:)