Replies: 2 comments
-
Thanks for looking into this. Can you post a stackblitz reproduction with what you have so far? |
Beta Was this translation helpful? Give feedback.
-
Here is a simple project where you can play around. There are 2 lists, one fully with Tanstack Query and the other with Don't forget to install with
To see the flashing effect you might want to CPU throttle in dev-tools in order to slow down the execution cycle. P.S. I don't know why but I cannot make the StackBlitz version to work, that is why I shared with bolt |
Beta Was this translation helpful? Give feedback.
-
After following the query.gg course and espcially the Optimistic-Update chapter, I was curious how we could use the new
useOptimistic
hook from React 19.As I could not find anything, I put my hands in the dirt and tried to use it with React Query.
Short answer
It doesn't fit well with React Query
Long answer
I asked chatGPT to make a summary of my findings
In trying out the new useOptimistic hook from React 19 with React Query, I found a few limitations that make it difficult to combine the two effectively:
useOptimistic
forces me to liftuseQuery
anduseMutate
to the same level, which becomes awkward if the query and mutation logic are in different components.addOptimisticItem
callback, a re-render seems to occur, restoring the initial state and canceling out the optimistic update. This undermines the intended behavior and makes it challenging to manage.Alternative Approaches
I also tried grouping
useQuery
anduseMutation
in a single hook to keep related data manipulation in one place, but re-render issues still interrupted the optimistic updates. This confirms for me that React Query’s cache control is more effective and thatuseOptimistic
seems better suited as an alternative rather than a complement.I have first put some comment in discord but I thought it was good to add my finding in a discussion in case other might ask themself the same question.
Feel free to add any of your findings with
useOptimistic
hook and React Query.Beta Was this translation helpful? Give feedback.
All reactions