繁体   English   中英

如何获得 react-native 中的 firebase 值?

[英]How get firebase value in react-native?

我想在构造函数发生时读取 firebase 的值并将其保存在 this.state 中。 我应该怎么办?

constructor(props) {
    super(props);
    var userId = firebase.auth().currentUser.uid;
    return firebase.database().ref("/users" + userId)
    .once("value").then(function(snapshot) {
      var uid = (snapshot.val() && snapshot.val().uid)
      var nickname = (snapshot.val() && snapshot.val().nickname)
    }),
    this.state = {
      //uid: firebase.auth().currentUser.uid,
      uid: "",
      nickname: "",

    };
  }

setState的调用需要在回调中:

firebase.database().ref("/users" + userId)
    .once("value").then(function(snapshot) {
      var uid = (snapshot.val() && snapshot.val().uid)
      var nickname = (snapshot.val() && snapshot.val().nickname)

      this.setState({
        uid: uid,
        nickname: nickname,    
      });
    }),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM