Best practices for using tanstack query with pinia store #2697
Replies: 1 comment 1 reply
-
Hello, The issue you're encountering with Here's how you can modify your use query configuration:
You will find the code I changed below: ....
const { data } = useQuery({
queryKey: ['randomDog'],
queryFn: fetchDog,
refetchOnWindowFocus: false,
refetchOnMount: false,
})
..... This configuration will prevent Additionally, if you want to control refetching behaviour even more precisely, you can look into other options provided by Solution in Stackblitz and Full Source Code
|
Beta Was this translation helpful? Give feedback.
-
If we use useQuery inside pinia store to fetch data that is then used inside getters for example,
useQuery
still sends requests after we leave the page and the data is no longer used.Is there a good way to prevent this from happening? Maybe there are guidelines on how to use data that is fetched by tanstack query inside pinia store?
Here is a small reproduction https://stackblitz.com/edit/vue3-vite-router-typescript-starter-1pyp7c?file=src%2FcounterStore.ts
when
useQuery
is used in a component the request wouldn't be refetched like it does when it's used in pinia. For example here i wouldn't expect on the About page that the request is sent:Screen.Recording.2024-06-17.at.16.32.52.mov
Beta Was this translation helpful? Give feedback.
All reactions