无法设置状态变量

时间:2017-03-24 00:07:29

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

我使用setState来更新程序中的2个状态。我的console.log能够显示2个更新的状态。但是,setState方法不起作用。年龄和性别这两个变量仍为空,不会在我的jsx中呈现。在此先感谢您的帮助

this.state={ age: '', gender: '' }........ //this.state provided


  fetchUserProfile(){
const { currentUser } = firebaseApp.auth();
firebaseApp.database().ref(`/users/${currentUser.uid}/profile`).orderByValue()
.on('child_added', snapshot => {
    console.log(snapshot.key, snapshot.val()); //successfully shows age and gender on console
    this.setState({
      age: snapshot.val().age,                 //does not update the state of age and gender
      gender: snapshot.val().gender
    })
})

}

1 个答案:

答案 0 :(得分:0)

您是否绑定了fetchUserProfile函数?

如果不只是改变:

fetchUserProfile(){fetchUserProfile = () => {

相关问题