2017-03-24 66 views
0

我用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 
    }) 
}) 

}

回答

0

你結合你的fetchUserProfile功能?

如果不只是改變:

fetchUserProfile(){fetchUserProfile =() => {

+0

您好馬特,我試圖綁定功能添加到構造函數,但它不工作。我也試過fetchUserProfile =()=>但也沒有成功。構造函數(){ 超() this.state = { 名: '', 手機: '', 電子郵件: '', 日期: '', 年齡: '', 性別: '' }, this.fetchUserProfile = this.fetchUserProfile.bind(this) } –

+0

若要測試它的工作方式,如果您在setState中更改年齡和性別年齡:10歲,性別:「男性」。它工作嗎? –

+0

嗨馬特,當我改變setState方法中的年齡和性別年齡:10和性別:'男',它仍然不起作用。 –