Skip to content

Conversation

@ebubecodes
Copy link
Contributor

Problem

The newNoteMutation's onSuccess handler uses string format for query keys ('notes'), but the useQuery hook uses array format (['notes']). In TanStack Query v4+, these are treated as completely different cache entries, causing the UI to not update after adding a new note.

Changes

  • Updated queryClient.getQueryData('notes')queryClient.getQueryData(['notes'])
  • Updated queryClient.setQueryData('notes', ...)queryClient.setQueryData(['notes'], ...)

Result

The mutation now correctly updates the same cache entry that useQuery is watching, enabling instant UI updates without triggering an extra GET request (as intended by the optimization in Part 6d).

Related

This fixes a bug in the course material implementation from 'Part 6d: React Query, useReducer and the context', "Optimizing the performance" section.

The useQuery hook uses an array format for queryKey (['notes']), but the
mutation's getQueryData and setQueryData calls were using a string format
('notes'). In TanStack Query v4+, these are treated as completely different
cache keys, preventing the optimistic cache update from working.

This caused the UI to not update after adding a new note because the mutation
was updating a different cache entry than the one being watched by useQuery.

Changed queryClient.getQueryData('notes') to queryClient.getQueryData(['notes'])
and queryClient.setQueryData('notes', ...) to queryClient.setQueryData(['notes'], ...)
to ensure cache consistency and proper UI updates.
@ebubecodes ebubecodes changed the title Fix: "Part 6d: React Query, ...: Optimizing the performance" - query key type mismatch in newNoteMutation cache update Fix: "Part 6d" - query key type mismatch in newNoteMutation cache update Oct 28, 2025
The useQuery hook uses an array format for queryKey (['notes']), but the
mutation's getQueryData and setQueryData calls were using a string format
('notes'). In TanStack Query v4+, these are treated as completely different
cache keys, preventing the optimistic cache update from working.

This caused the UI to not update after adding a new note because the mutation
was updating a different cache entry than the one being watched by useQuery.

Changed queryClient.getQueryData('notes') to queryClient.getQueryData(['notes'])
and queryClient.setQueryData('notes', ...) to queryClient.setQueryData(['notes'], ...)
to ensure cache consistency and proper UI updates.
@ebubecodes
Copy link
Contributor Author

I pushed a follow-up commit to this branch that addresses the same issue, but in the Finnish translation. No other semantic changes. Thanks!"

@vejol vejol merged commit 71fc151 into fullstack-hy2020:source Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants