React native [TypeError:undefined不是对象(正在计算此.setState';)]
我对ReactNative和axios有问题。我想将响应URL保存在状态变量中,但我遇到以下问题:React native [TypeError:undefined不是对象(正在计算此.setState';)],react-native,axios,react-native-android,React Native,Axios,React Native Android,我对ReactNative和axios有问题。我想将响应URL保存在状态变量中,但我遇到以下问题: [类型错误:未定义不是对象(计算'this.setState')] 职能: githubGetUrl = () => { axios({ method: 'get', url: 'http://' + global.IP_ADDRESS + ':8080/github/link' }).then(function(response) {
[类型错误:未定义不是对象(计算'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: ''
}
}
我已经在网上搜索了,但是我没有找到问题的根源
如果有人能帮助我,我将不胜感激
谢谢您可以尝试在
中使用箭头功能,然后
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)
});
}
}
setState
是一个异步进程,因此将您的console.log
作为回调。我该如何做?this.setState({githuburl:response.data.LINK},()=>console.log('Résultat['+this.state.githuburl+'])