Skip to content
Source
Sign inSign up
TanStack/query

React Query Cache Not Updating – Requires Manual Invalidation via useFocusEffect #9102

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

Describe the bug

In my React Native app, the Portfolio screen displays a list of investment cards. Tapping on any card navigates to the InvestmentDetails screen, where a user can sell the investment using the useRequestInvestmentSell hook.

βœ… Expected Behavior: After submitting a sell request:

1- InvestmentDetails screen updates:

  • Shows a flag

  • Reflects the updated list of sell requests

2- Portfolio screen updates:

  • The sold investment card displays a flag indicating a pending sell request

To achieve this, I'm invalidating the following queries after submission:

  • INVESTMENT_SELL_REQUESTS (for the request list)

  • CUSTOMER_BANK_INVESTMENTS_DETAILS

  • CUSTOMER_BANK_INVESTMENTS (cards rendered in the Portfolio)

❌ Actual Behavior: When navigating back to the Portfolio screen, the data does not update as expected. The only workaround that currently works is manually calling invalidateQueries(CUSTOMER_BANK_INVESTMENTS) inside a useFocusEffect() hook on the Portfolio screen β€” but this feels like a hack and is not ideal for performance or code cleanliness. 😞

export const useRequestInvestmentSell = () => {
  const queryClient = useQueryClient()

  return useMutation(bankApi.requestInvestmentSell, {
    onSuccess: () => {
      queryClient.invalidateQueries(INVESTMENT_SELL_REQUESTS)
      queryClient.invalidateQueries(CUSTOMER_BANK_INVESTMENTS_DETAILS)
      queryClient.invalidateQueries(CUSTOMER_BANK_INVESTMENTS)
    },
  })
}

Your minimal, reproducible example

Steps to reproduce

Expected behavior

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

Tanstack Query adapter

None

TanStack Query version

TypeScript version

No response

Additional context

No response

  1. 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.