2016-10-03 2 views
0

だから私はナビゲーションバーを持っていると私は彼らがサインとなステートメント場合、私はこれを持っている場合、それはユーザーの名前を表示したい:React:get要求の中でどのように状態を設定するのですか?

var that= this; 
this.state = {name: null, signedIn: false} 
if (user){ 
     axios.get('https://apiendpoint/' + user.id) 
     .then(function (response) { 
      console.log(response); 
      that.state = {name: response.data.firstName, signedIn: true} 
      //this.setState({ showModal: false }); 
     }) 
     .catch(function (error) { 
      console.log(error); 
     }); 
     } 

if文が実行されているが、状態変数がない場合設定されているようです。 var that =thisは状態変数を設定することができませんか、これを行うにはより良い方法がありますか?

答えて

0

私はthis.state = {...}の代わりにthat.setState({...});を使用しなければならなかった

関連する問題