admin管理员组文章数量:1333450
I'm fetching data on ponentDidMount
in fetchData
method. After that I'm trying to delete data with the method. I was trying to update date immediately in deleteUser
method, but it doesn't work. How can I update this data after fetch with delete method?
fetchData = () => {
let url = ``;
fetch(url, {method: 'get'}).then(resp => resp.json()).then((data) =>
this.setState({
data: Object.entries(data).map(([key,value])=>({key:Number(key),value}))
})
);
};
ponentDidMount() {
this.fetchData();
}
deleteUser = (id) => {
let url = `/${id}`;
fetch(url, {method: 'delete'}).then(resp => console.log(resp));
this.fetchData();
};
I'm fetching data on ponentDidMount
in fetchData
method. After that I'm trying to delete data with the method. I was trying to update date immediately in deleteUser
method, but it doesn't work. How can I update this data after fetch with delete method?
fetchData = () => {
let url = `https://contact-browser.herokuapp./contacts`;
fetch(url, {method: 'get'}).then(resp => resp.json()).then((data) =>
this.setState({
data: Object.entries(data).map(([key,value])=>({key:Number(key),value}))
})
);
};
ponentDidMount() {
this.fetchData();
}
deleteUser = (id) => {
let url = `https://contact-browser.herokuapp./contact/${id}`;
fetch(url, {method: 'delete'}).then(resp => console.log(resp));
this.fetchData();
};
Share
Improve this question
edited Nov 5, 2023 at 22:56
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jan 31, 2019 at 12:10
JohnJohn
31 silver badge2 bronze badges
2
- 1 When you say 'it didn't work', what does happens as opposed to what would you like to happen? – Predrag Beocanin Commented Jan 31, 2019 at 12:12
- It deletes user, but data is the same. – John Commented Jan 31, 2019 at 12:13
3 Answers
Reset to default 6Refetch your data after the delete fetch has fulfilled, this ensures that the data is fetched after the delete has fully resolved on the server.
deleteUser = (id) => {
let url = `https://contact-browser.herokuapp./contact/${id}`;
fetch(url, {method: 'delete'}).then(resp => {
this.fetchData();
});
};
It "did not work" because of the async behaviour of JS. To be simple, when there is any waits in the code (like DB Call, API Call, etc), the JS code lets that particular to run and starts executing the next line. More on this - https://www.hongkiat./blog/synchronous-asynchronous-javascript/
In this case, since fetch is an i/o wait, the control moves to the next line - this.fetchData()
. So, the fetchData()
is called before delete may actually happens.
Following is another solution using async and await. More to read
deleteUser = async (id) => {
let url = `https://contact-browser.herokuapp./contact/${id}`;
let resp = await fetch(url, {method: 'delete'});
console.log(resp);
this.fetchData();
};
You can also use async and await
deleteUser = async (id) => {
let url = `https://contact-browser.herokuapp./contact/${id}`;
const deleted = await fetch(url, {method: 'delete'}).then(resp => console.log(resp));
if(deleted){
await this.fetchData();
}
};
本文标签: javascriptHow to update data after fetchStack Overflow
版权声明:本文标题:javascript - How to update data after fetch? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742352655a2458798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论