Skip to content

Commit 30caf0f

Browse files
authored
Update queryClient calls to use array keys
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.
1 parent 05ccb3c commit 30caf0f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/content/6/fi/osa6d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ const App = () => {
380380
mutationFn: createNote,
381381
// highlight-start
382382
onSuccess: (newNote) => {
383-
const notes = queryClient.getQueryData('notes')
384-
queryClient.setQueryData('notes', notes.concat(newNote))
383+
const notes = queryClient.getQueryData(['notes'])
384+
queryClient.setQueryData(['notes'], notes.concat(newNote))
385385
// highlight-end
386386
}
387387
})

0 commit comments

Comments
 (0)