【发布时间】:2018-06-02 18:12:50
【问题描述】:
我对 react 很陌生,可能犯了一个愚蠢的错误。我正在尝试使用返回承诺的 axios 进行 api 调用。当这个承诺解决时,我想将它的结果传递给一个函数,该函数通过回调更新父级的状态。但是,当我不确定时,“这个”似乎消失了。我想随着它在未来的解决,不再有“这个”上下文?我可以通过将回调分配给临时并使用它来解决它,但感觉很笨拙。代码如下:
axios.get(url)
.then(function(response) {
this.props.handler(response.data.thing)
})
.catch(function(error) {
console.log(error)
})
这行得通:
let handler = this.props.handler
axios.get(url)
.then(function(response) {
handler(response.data.word)
})
.catch(function(error) {
console.log(error)
})
【问题讨论】:
标签: javascript reactjs ecmascript-6 es6-promise axios