2016-10-03 105 views
0

所以我有一個導航欄,我想如果他們在簽署顯示用戶的名字,我有這個,如果statment:if語句運行反應:如何在獲取請求中設置一個狀態?

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); 
     }); 
     } 

的,但是狀態變量不似乎正在設置。 var that =this沒有工作來設置狀態變量或有沒有更好的方法來做到這一點?

回答

0

想通了。我不得不使用that.setState({...});而不是this.state = {...}

相關問題