gpt4 book ai didi

javascript - Axios.put ReactJS

转载 作者:行者123 更新时间:2023-12-04 09:40:48 25 4
gpt4 key购买 nike

它有点工作,但问题是它复制正在更新的文件并将其放在页面的末尾,我正在尝试对项目进行显示更新

这就是我想要做的

    submitEdit = (id, value) => {
let {todos} = this.state
todos.map((item => {
if (item._id === id) {
axios
.put(`http://localhost:8080/edit/${id}`, {
todo: value,
})
.then((res) => {
this.setState({
todos:[...todos,{todo:value}]
})
console.log("res", res);
})
.catch((err) => {
console.log("err", err);
});
}
}))
}

除此之外一切正常

最佳答案

需要使用索引来更新状态,这样todo元素才会被更新,而不是被复制并添加到最后

您可以使用 Array.prototype.slicespread syntax要做到这一点

todos.map(((item, i) => {
if (item._id === id) {
axios
.put(`http://localhost:8080/edit/${id}`, {
todo: value,
})
.then((res) => {
this.setState({
todos:[...todos.slice(0, i),{todo:value}, ...todos.slice(i + 1)]
})
console.log("res", res);
})
.catch((err) => {
console.log("err", err);
});
}
}))

关于javascript - Axios.put ReactJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62344028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com