[TypeError: undefined is not an object (evaluating 'this.setState')]

[TypeError: undefined is not an object (evaluating 'this.setState')]

我遇到 ReactNative 和 axios 的问题。我想将响应 URL 保存在状态变量中,但我遇到了这个问题:

[TypeError: undefined is not an object (evaluating 'this.setState')]

函数:

githubGetUrl = () => {
    axios({
      method: 'get',
      url: 'http://' +  global.IP_ADDRESS + ':8080/github/link'
    }).then(function(response) {
      this.setState({githuburl: response.data.LINK})
      console.log('Résultat [' + this.state.githuburl + ']')
    }).catch(function(error) {
      console.log(error)
    });
  }
}

构造函数:

constructor (props) {
  super(props)
  this.state = {
    clicked: '',
    githuburl: ''
  }
}

我已经在网上搜索了,但没有找到问题的根源...

如果有人能帮助我,我将不胜感激。

谢谢

你可以尝试在then

中使用箭头函数
githubGetUrl = () => {
    axios({
      method: 'get',
      url: 'http://' +  global.IP_ADDRESS + ':8080/github/link'
    }).then((response) => {
      this.setState({githuburl: response.data.LINK})
      console.log('Résultat [' + this.state.githuburl + ']')
    }).catch(function(error) {
      console.log(error)
    });
  }
}