Skip to content
Source
Sign inSign up
TanStack/query

queryClient.setQueryData() not updating UI consistently after mutation #9476

Send to my agent
Closedvadaviya638(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

queryClient.setQueryData() not updating UI consistently after mutation

I have data in the tenderGetChatSession query, but nothing is shown in the console.

i have two console please see

useQuery (for chat sessions)

const tenderGetChatSession = useQuery({
  queryKey: ['tenderGetChatSession', tenderId],
  queryFn: async () => {
    return await backendApi.tenderAiAgentChatApi.getAllAiChats(tenderId).then(res => res.data)
  },
  refetchOnWindowFocus: false,
  refetchOnMount: false,
  enabled: !!tenderId
})

useMutation (to create chat session)

const tenderCreateChatSession = useMutation({
  mutationFn: async () => {
    return await backendApi.tenderAiAgentChatApi.createChatSession(tenderId, { name: inputValue })
  },
  onMutate: async () => {
    await queryClient.cancelQueries({ queryKey: ['tenderGetChatSession', tenderId] })
    const previousSessions = queryClient.getQueryData(['tenderGetChatSession', tenderId])
    return { previousSessions }
  },
  onSuccess: (session) => {
    console.log(
  'before mutation setQueryData:',
  queryClient.getQueryData(['tenderGetChatSession', tenderId])
)
    queryClient.setQueryData(['tenderGetChatSession', tenderId], (old = []) => {
      if (!Array.isArray(old)) return [session?.data]
      const filtered = old.filter(s => s.sessionId !== session.data.sessionId)
      return [session.data, ...filtered]
    })
    console.log(
  'After mutation setQueryData:',
  queryClient.getQueryData(['tenderGetChatSession', tenderId])
)
  },
  onError: (err, _newSession, context) => {
    queryClient.setQueryData(['tenderGetChatSession', tenderId], context?.previousSessions)
  },
  onSettled: () => {
    queryClient.invalidateQueries({ queryKey: ['tenderGetChatSession', tenderId], refetchType: 'inactive' })
  }
})

console before mutation setQueryData: undefined After mutation setQueryData: [ { "sessionId": 1184, "name": "What is the estimated value of this tender?" } ]

sometimes the UI does not update with the newly created session, even though: The POST API call is successful The onSuccess callback runs It behaves inconsistently — sometimes it works, sometimes it doesn't.

Your minimal, reproducible example

NA

Steps to reproduce

What I’ve Tried

  • Logging getQueryData before/after mutation – sometimes it's undefined or empty.

  • Disabling background refetch (refetchOnMount: false, refetchOnWindowFocus: false)

Expected behavior

🙏 Request Can someone from the team please confirm:

Is this expected behavior?

Is there something wrong with how I'm using setQueryData or invalidateQueries?

Any guidance on making sure UI reflects updated cache immediately?

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

chrome

Tanstack Query adapter

None

TanStack Query version

5.5

TypeScript version

No response

Additional context

No response

  1. TkDodocommented

    Thank you for filing this issue, but we need more information before looking into this.

    Specifically, a minimal, standalone, runnable reproduction is needed - preferably with codesandbox or stackblitz. If your issue is TypeScript related, prefer a TypeScript playground.

    We'll close this issue for now, but we'll certainly re-open it if there is a proper reproduction.

  2. TkDodo closed this

  3. TkDodo added labelsneeds-info

Sign in to join this conversation.