T
TanStack•3y ago
absent-sapphire

Does `setQueryData` really re-renders component?

Hey, im struggling with very similar issue, and I don't understand why it works with native useState but not with setting query data manually as optimistic update" https://github.com/bjornhenriksson/dnd-react-query-problem
GitHub
GitHub - bjornhenriksson/dnd-react-query-problem: Showcases issues ...
Showcases issues with mutating cache optimistically with react-query and dnd-kit. - GitHub - bjornhenriksson/dnd-react-query-problem: Showcases issues with mutating cache optimistically with react-...
6 Replies
absent-sapphire
absent-sapphireOP•3y ago
and indeed - it works only with that strange randomState set, maybe setQueryData does not re-render component? Hey @TkDodo 🔮 maybe you have some insights on this? Any comments would be helpful from core team! 🙂
quickest-silver
quickest-silver•3y ago
setQueryData definitely re-renders the component, unless you make updates in a mutable way: https://tanstack.com/query/v4/docs/react/guides/updates-from-mutation-responses#immutability
Updates from Mutation Responses | TanStack Query Docs
When dealing with mutations that update objects on the server, it's common for the new object to be automatically returned in the response of the mutation. Instead of refetching any queries for that item and wasting a network call for data we already have, we can take advantage of the object returned by the mutation function and update the exis...
absent-sapphire
absent-sapphireOP•3y ago
thats odd. I return new immutable object from setQueryData. The problem is that only useState setter is rerendering it without flickering.
fair-rose
fair-rose•2y ago
I also have this issue I used JSON.parse + stringify to clone original data, so it's immutable but it doesnt trigger a rerender, if I force a render, I can see the updated value though.
quickest-silver
quickest-silver•2y ago
show a codesandbox please
fair-rose
fair-rose•2y ago
Looks like only happens with useInfiniteQuery I'll make a sandbox Sorry, I can't reproduce it on a simpler environment. Maybe it's something very specific to my setup, I don't think it's related to immutability though.

Did you find this page helpful?