javascript - Close modal on successful axios post
问题描述
I've tried changing the state when you manage to do a successful post without any further success.
My Post Method
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")
})
}
my state
state = {
open: false /* if true, modal is open */
}
close modal method
close = () => this.setState({ open: false })
Any suggestions on how to close the modal on successful axios post would be much appreciated.
解决方案
正如@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")
})
}
推荐阅读
- azure - 如何使用 Azure Cosmosdb SDK 创建无服务器 cosmosdb?
- python - 用理解创建的字典的深层副本不起作用
- python-3.x - 使用位置和关键字参数的顺序
- jquery - 如何从其他表中获取值并在数据表中显示
- redux - React-Redux useSelector 无法传递数据
- python - 在 Python 中的类之外定义类方法
- java - Java Web Start 拒绝自签名证书
- javascript - 'self' 脚本被阻止加载,但是当我使用 nonce 时它们可以工作吗?
- python - Python 长度不匹配
- javascript - 使用 JavaScript 制作添加到收藏夹按钮