TanStackT
TanStack9mo ago
12 replies
popular-magenta

How to access individual customer by ID after a search with TanStack Query?

Hi, working on a React Native app with Expo Router + TanStack Query and could use some advice on caching patterns.

what i have:
const searchCustomersQueryOptions = (search: string) =>
  queryOptions({
    queryKey: ["customers", search],
    queryFn: () => getCustomers(search),
  });

export const useSearchCustomers = (search: string) => {
  return useQuery(searchCustomersQueryOptions(search));
};


In another screen, I allow selecting a customer and passing only their id back. Later, I want to fetch the full customer data using just the ID, ideally from the query cache .

Because customers are cached under ['customers', search], I can't access them directly by ['customer', id] later — unless I search thru that key wich is meh..

any suggestion? Idk if this is right but ideally i would need ['customer', customer.id]
Was this page helpful?