2017-03-24 17 views
0

setStateを使用してプログラムの2つの状態を更新しました。私のconsole.logは2つの更新された状態を表示することができました。ただし、setStateメソッドは機能しません。年齢と性別の2つの変数は空のままで、私の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メソッド内の年齢と性別を変更すると、それはまだ動作しません。 –

関連する問題