2016-10-03 84 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 = {...}

相关问题