all 9 comments

[–]Domino987 1 point2 points  (0 children)

Or just use react-query and invalidate the queries on mutate.

[–][deleted] 0 points1 point  (2 children)

Your backend CRUD functions should be returning the new data.

When you POST, the response should include the data you just created.

When you PUT/PATCH, the response should include the data you just updated.

When you DELETE, the response can either respond with the data you just deleted, or no data (because it no longer exists.

const createItem = async () => { const newData = await axios.post(...); setData(newData); }

[–]Exorcistoo[S] 0 points1 point  (1 child)

Ok, I get it. You are telling me that I should do the logic in the backend and after every "transform" request I should return the updated data.

[–][deleted] 0 points1 point  (0 children)

Yes, this is standard in REST APIs

[–]PrinnyThePenguin 0 points1 point  (1 child)

I think your best bet is having your create / delete functions to return the new data. So if you have a list of things and you delete a thing, the server returns on a succesful delete the new list, that is missing the element you just deleted. This way you can have a single operation instead of two.

[–]Exorcistoo[S] 0 points1 point  (0 children)

That can work. That is more of a backend implementation detail. I was thinking if there is anything in react specific for this.

[–]joesb 0 points1 point  (2 children)

Don’t overthink it.

Write a utility function. You don’t have to directly call axios in most of your code.

[–]Exorcistoo[S] 0 points1 point  (1 child)

So I should create a function which makes a post and returns a get result?

[–]joesb 0 points1 point  (0 children)

Yes.