T
TanStack2y ago
optimistic-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
exotic-emerald
exotic-emerald2y ago
Where is queryClient coming from?
optimistic-gold
optimistic-gold2y 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.
optimistic-gold
optimistic-goldOP2y ago
its issue, invalidate must fetch query again but it didnt do that
optimistic-gold
optimistic-goldOP2y ago
GitHub
jobspot/src/features/core/components/Providers.tsx at master · Szym...
Contribute to SzymonG110/jobspot development by creating an account on GitHub.
optimistic-gold
optimistic-gold2y 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
optimistic-gold
optimistic-goldOP2y ago
if i have only invalidate it still didnt work
optimistic-gold
optimistic-gold2y 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
optimistic-gold
optimistic-goldOP2y ago
so what client i must use? const queryClient = useQueryClient() this?
optimistic-gold
optimistic-gold2y ago
Probably, yes
optimistic-gold
optimistic-goldOP2y ago
ok, thx @troywoystill doesnt work
optimistic-gold
optimistic-gold2y 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!
optimistic-gold
optimistic-goldOP2y ago
No description
optimistic-gold
optimistic-goldOP2y ago
nevermind i do that i can close this thx guys

Did you find this page helpful?