Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React useInfiniteQuery & useQuery caching issue #8617

Open
Karinza38 opened this issue Feb 6, 2025 · 1 comment
Open

React useInfiniteQuery & useQuery caching issue #8617

Karinza38 opened this issue Feb 6, 2025 · 1 comment

Comments

@Karinza38
Copy link

Discussed in #8616

Originally posted by ktriviso February 7, 2025
Hi, my React app is using both useQuery and useInfiniteQuery. When using react-query-devtools, I can see the cache is being set and cleared with no issues for queries that used useQuery to fetch the data. But any data fetched using useInfiniteQuery is not setting the cache and I am not able to use invalidateQueries to clear it either. We were previously using a SUPER old version of React query (not Tanstack) and this pattern was working.

I have tried various different solutions with no luck. Below is the exact scenario I am trying to solve for and my current implementation.

Scenario:
Query list of orders. In a modal, delete an order and invalidate the query so that it refreshes and the list of orders does not contain the deleted order. Important to note, the modal does not have access to the useInfiniteQuery refetch method.

Current code:
List page - using this solution, I can see the cache is set in react-query-devtools but it's inactive

const {
  data,
  hasNextPage,
  fetchNextPage,
  isFetching,
  status,
  refetch,
} = useInfiniteQuery({
  queryKey: ["get_orders"],
  queryFn: ({ pageParam = 1 }) =>
  callBackend(ENDPOINT, { method: "GET" }),
  staleTime: 0,
  getNextPageParam: (d, _) => {
    const { current_page, total_pages } = d;
    d.nextCursor;
    // POSSIBLE FIX FOR CACHE NOT BEING SET
    cache.setQueryData(["get_orders"], d);
    return current_page < total_pages ? current_page + 1 : undefined;
  },
  getPreviousPageParam: (d, _) => d.prevCursor,
});

Delete modal

  const mutation = usePalomaMutation(ENDPOINT, "post", null, {
    onSuccess: (res) => {
      cache.invalidateQueries({ queryKey: ["get_orders"] });
    },
  });
```</div>
@gs18004
Copy link

gs18004 commented Feb 7, 2025

Can I see the full code?
with something like code sandbox, etc.

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

No branches or pull requests

2 participants