Best practices for mutating the local cache for PATCH/PUT and DELETE #567
Unanswered
KonigTibor
asked this question in
General
Replies: 2 comments 2 replies
-
Try to pass a function to mutate, this function will receive the current data, you can then use it for the spread. mutate(key, currentData => ({ ...currentData, items: [ ...currentData.items.slice(0, index), index < data.items.length - 1 && data.items.slice(index + 1) ] }), false) Something like that. |
Beta Was this translation helpful? Give feedback.
2 replies
-
Try to add a ID in the key .Map loop instead of index |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Dear All,
I have a list of items coming from a REST API, displayed in a list (or table, doesn't matter). When I make modifications I would like to get a fast and smooth rendering so I use mutation. Adding a new item works perfectly but after updating or removing an item I have problems with the rendering. So far, I tried two methods listed below, please take a look and advise what I should change.
A couple of quick notes for the code: key and data come from SWR, items is an array of items returned by the API, index is the index of the item to be modified/removed, and updatedItem is the new value of the item to be modified.
Splice
Problem: After the following type of mutation, the whole list of items gets rerendered.
When updating:
When deleting:
Slice
Problem: After the following type of mutation, the changes get rendered in a strangely staggered manner. The line containing the modified item moves to the end of the list then jumps back to its correct position. The line containing the deleted item also disappears in two visible steps.
Observation: It seems that during this one mutate() call, the items array is changed several types.
When updating:
When deleting:
Any help greatly appreciated.
TK
Beta Was this translation helpful? Give feedback.
All reactions