T
TanStack3w ago
cloudy-cyan

invalidating the first key doesn't work

in tanstack query, what are the possible reason why invalidating ["user"] doesn't invalidate ["user", "some-username", "posts"]? i'm sure that i'm using correctqueryClientas well
// useQuery
const threadsQuery = useInfiniteQuery(userThreadsInfiniteOptions(username));
// useQuery
const threadsQuery = useInfiniteQuery(userThreadsInfiniteOptions(username));
// options
export const userOptions = queryOptions({
queryKey: ["user"],
});

export const userThreadsInfiniteOptions = (username: string) => {
return infiniteQueryOptions({
queryKey: [userOptions.queryKey, username, threadInfiniteOptions.queryKey],
initialPageParam: undefined,
queryFn: async ({ pageParam }: { pageParam: string | undefined }) =>
getUserThreads({
username,
lastId: pageParam,
}),
getNextPageParam: (posts) => {
if (posts.length === 0) return undefined;

return posts[posts.length - 1].id;
},
});
};
// options
export const userOptions = queryOptions({
queryKey: ["user"],
});

export const userThreadsInfiniteOptions = (username: string) => {
return infiniteQueryOptions({
queryKey: [userOptions.queryKey, username, threadInfiniteOptions.queryKey],
initialPageParam: undefined,
queryFn: async ({ pageParam }: { pageParam: string | undefined }) =>
getUserThreads({
username,
lastId: pageParam,
}),
getNextPageParam: (posts) => {
if (posts.length === 0) return undefined;

return posts[posts.length - 1].id;
},
});
};
// invalidation
queryClient.invalidateQueries({
queryKey: userOptions.queryKey,
});
// invalidation
queryClient.invalidateQueries({
queryKey: userOptions.queryKey,
});
2 Replies
sensitive-blue
sensitive-blue3w ago
queryKey: [userOptions.queryKey, username, threadInfiniteOptions.queryKey], doesn't userOptions.queryKey make your queryKey here [["user"], "skidy", …] you could spread the userOptions.queryKey in
queryKey: [...userOptions.queryKey, username, ...threadInfiniteOptions.queryKey],
queryKey: [...userOptions.queryKey, username, ...threadInfiniteOptions.queryKey],
cloudy-cyan
cloudy-cyanOP3w ago
thank you so much! i never thought about that, you saved me a day!

Did you find this page helpful?