T
TanStack•2y ago
dependent-tan

invalidateQueries

Why is the incorrect way of invalidating working and the what I believe is the correct way not?
const result = useQuery({
queryKey: [WORKOUT_CACHE_KEY, ['/set'], id],
queryFn: () => getSets(id),
initialData: [defaultValues],
staleTime: 1000,
});

const queryClient = useQueryClient();

async function handleDeleteSet(setId: number) {
await deleteSet(setId);
//queryClient.invalidateQueries({ queryKey: ['/set'] }); <-- doesn't work
queryClient.invalidateQueries('/set'); // does work
}
const result = useQuery({
queryKey: [WORKOUT_CACHE_KEY, ['/set'], id],
queryFn: () => getSets(id),
initialData: [defaultValues],
staleTime: 1000,
});

const queryClient = useQueryClient();

async function handleDeleteSet(setId: number) {
await deleteSet(setId);
//queryClient.invalidateQueries({ queryKey: ['/set'] }); <-- doesn't work
queryClient.invalidateQueries('/set'); // does work
}
Also tried a staleTime: 0
4 Replies
adverse-sapphire
adverse-sapphire•2y ago
weird. which version?
dependent-tan
dependent-tanOP•2y ago
Version 5.14...
adverse-sapphire
adverse-sapphire•2y ago
okay your key is this:
[WORKOUT_CACHE_KEY, ['/set'], id],
[WORKOUT_CACHE_KEY, ['/set'], id],
so this can't work queryKey: ['/set'] because arrays have an order the bare sting likely works by accident becaus it will just target all queries
dependent-tan
dependent-tanOP•2y ago
Thanks my bad... Got it working 🙂

Did you find this page helpful?