T
TanStack15mo ago
national-gold

Invalidate didnt work

I have mutation
const { mutate, isPending } = useMutation({
mutationFn: (values: SignInUser) => signIn(values),
onSuccess: async (data) => {
if (data.ok) {
await queryClient.invalidateQueries({
queryKey: ["userSession"],
});
await queryClient.refetchQueries({
queryKey: ["userSession"],
});
router.push("/");
}
},
});
const { mutate, isPending } = useMutation({
mutationFn: (values: SignInUser) => signIn(values),
onSuccess: async (data) => {
if (data.ok) {
await queryClient.invalidateQueries({
queryKey: ["userSession"],
});
await queryClient.refetchQueries({
queryKey: ["userSession"],
});
router.push("/");
}
},
});
and i have in navbar query
const { data: userSession = null, ...rest } = useQuery({
queryKey: ["userSession"],
queryFn: () => session(),
gcTime: cacheTime,
});
const { data: userSession = null, ...rest } = useQuery({
queryKey: ["userSession"],
queryFn: () => session(),
gcTime: cacheTime,
});
after successfully login my userSession is still null and i must refresh page by f5
No description
14 Replies
fascinating-indigo
fascinating-indigo15mo ago
Where is queryClient coming from?
extended-salmon
extended-salmon15mo ago
It's not the issue, but you don't need to invalidate and refetch it'll refetch for you.
The invalidateQueries method can be used to invalidate and refetch single or multiple queries in the cache based on their query keys or any other functionally accessible property/state of the query. By default, all matching queries are immediately marked as invalid and active queries are refetched in the background.
national-gold
national-goldOP15mo ago
its issue, invalidate must fetch query again but it didnt do that
national-gold
national-goldOP15mo ago
GitHub
jobspot/src/features/core/components/Providers.tsx at master · Szym...
Contribute to SzymonG110/jobspot development by creating an account on GitHub.
extended-salmon
extended-salmon15mo ago
Which is why vossmalte asked you where your queryClient came from. Calling invalidate and then refetch means you'd probably have two network fetches is all I was commenting on, just a performance improvement
national-gold
national-goldOP15mo ago
if i have only invalidate it still didnt work
extended-salmon
extended-salmon15mo ago
What vossamalte was eluding to was a wrong queryClient reference. If somehow you are creating a new QueryClient() and it isn't stable, then you'd be calling invalidate on a wrong client
national-gold
national-goldOP15mo ago
so what client i must use? const queryClient = useQueryClient() this?
extended-salmon
extended-salmon15mo ago
Probably, yes
national-gold
national-goldOP15mo ago
ok, thx @troywoystill doesnt work
extended-salmon
extended-salmon15mo ago
I know invalidateQueries most certainly does work because we use it in our app, but I can't look through that entire repo to debug this. Maybe trying debugger or console.log to check the queryClient reference you're trying to call invalidate on. The devtools are also a huge help: https://tanstack.com/query/latest/docs/framework/react/devtools
Devtools | TanStack Query React Docs
Wave your hands in the air and shout hooray because React Query comes with dedicated devtools! 🥳 When you begin your React Query journey, you'll want these devtools by your side. They help visualize all the inner workings of React Query and will likely save you hours of debugging if you find yourself in a pinch!
national-gold
national-goldOP15mo ago
No description
national-gold
national-goldOP15mo ago
nevermind i do that i can close this thx guys

Did you find this page helpful?