Let's say I have multiple functions that change data in the back-end and I want to have a page with CRUD features. On that page I want to be able to create/delete the data and display it. Every time I create an item or delete one, I fetch the data and display it, so it stays updated.
Currently, in the create/delete functions I also call fetch at the end. But if I have multiple functions which modify the data I need to call fetch in every function.
const createItem = async () => {
await axios.post(...);
const newData = await axios.get(...);
setData(newData);
}
const deleteItem = async () => {
await axios.delete(...);
const newData = await axios.get(...);
setData(newData);
}
Is there any way I can call a fetch data function after every function that changes data?
You could move the newData / setData(newData) to a separate function and call that after createItem and deleteItem have successfully completed.
If you create new functions that need to get the new data after completing you can use it there too.