我试过在你成功完成一篇帖子但没有进一步成功的情况下改变状态。
我的Post方法
saveOnClick = () => {
axios.put('http://localhost:8080/lagbevakning/revision', {
revisionId: 36,
subscriptionId: 21549450,
})
.then(function (response) {
alert("You've sucessfully managed to do that")
/* here i wish to close the modal */
})
.catch(function (error) {
console.log(error)
alert("something went wrong")
})
}我的状态
state = {
open: false /* if true, modal is open */
}封闭模态法
close = () => this.setState({ open: false })
任何关于如何在成功的axios帖子上关闭模式的建议都将非常感谢。
发布于 2019-04-16 18:45:12
正如@Quentin指出的那样,then内部的上下文将是错误的。因此,您需要将this的引用保存在一个单独的变量(或常量)中。
执行以下操作:
saveOnClick = () => {
const self = this;
axios.put('http://localhost:8080/lagbevakning/revision', {
revisionId: 36,
subscriptionId: 21549450,
})
.then(function (response) {
alert("You've sucessfully managed to do that")
/* here */ self.close();
/* here i wish to close the modal */
})
.catch(function (error) {
console.log(error)
alert("something went wrong")
})
}发布于 2020-06-13 09:41:58
使用箭头函数而不是使用函数调用
https://stackoverflow.com/questions/55706122
复制相似问题