【问题标题】:How to display date in firebase with react native如何使用本机反应在firebase中显示日期
【发布时间】:2020-02-28 08:48:15
【问题描述】:

大家! 我已经将我的应用程序与 firebase 连接,但我无法在我的应用程序上显示用户数据: 能否请您看一下我的代码:

componentDidMount() {
firebase.auth().onAuthStateChanged(user => {
  if(user!=null){
    userId = firebase.auth().currentUser.uid
  }else {
    this.props.navigation.navigate("Loading");
  }
      }),
firebase.database().ref('Users/').on('value', function (snapshot) {
     console.log(snapshot.val()); //this work
  // let name = snapshot.val().name;
  // this.setState ({users :name,}) //this not work
  }); 
    }

......和

<View style={styles.contain}>
        <ProfileDetail
          // image={userData.image}
          // textFirst={users.name}
          // point={userData.point}
          textSecond={this.state.name} //. i'd like to display the child on this
          textThird={currentUser.email}
          // onPress={() => navigation.navigate("ProfileExample")}
        />

我的代码有什么问题吗? 请帮帮我!

【问题讨论】:

    标签: javascript reactjs firebase react-native firebase-realtime-database


    【解决方案1】:

    改变这个:

    firebase.database().ref('Users/').on('value', function (snapshot) {
         console.log(snapshot.val()); //this work
      // let name = snapshot.val().name;
      // this.setState ({users :name,}) //this not work
      }); 
    

    进入这个:

    firebase.database().ref('Users/').on('value', ((snapshot) => {
         console.log(snapshot.val()); //this work
         let name = snapshot.val().name;
         this.setState ({users :name,})
      }); 
    

    使用arrow function 而不是声明普通函数。

    箭头函数没有自己的 this。使用封闭词法范围的 this 值;箭头函数遵循正常的变量查找规则。因此,在搜索当前作用域中不存在的 this 时,箭头函数最终会从其封闭作用域中找到 this。

    【讨论】:

    • 谢谢!它不会警告错误,但我正在使用这个'textSecond = {this.state.name}'它不显示
    • 什么? @MinhTiếnPhạm
    • 还是有问题,它不工作,我称之为 this.state.users 但未完善
    • 你把它添加到状态中了吗
    • 很抱歉我迟到的答案,但它的工作方式是另一种方式。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    相关资源
    最近更新 更多