TanStackT
TanStack3y ago
7 replies
popular-magenta

Optimistic updates with sorted lists

For a list of todos sorted by due date, what is the best way to handle optimistic updates for a new to do?

https://tkdodo.eu/blog/mastering-mutations-in-react-query mentions it as one of those cases where things can get complex fairly quickly but it is a required feature for what I'm building..

My guess is that you add it to the cache and run a local sort. This seems fine....? Am I missing something.

Also, one other question - for optimistic updates, I am setting the temporary id as Math.random(). This required me to change the type of ToDoData from {id: string} to {id?: string} so that I know when the id is undefined and I can prevent actions like clicking the to do to see a detailed view. Is this the best practice? Kinda sucks that my type has id as optional just to accomodate optimistic updates.
Was this page helpful?