-
To query user posts in multiple components that are mounted at the same time, i tried to wrapper defineQuery like below: export const useUserPostsQuery = defineQuery(() => {
console.log("defineQuery executed");
const route = useRoute();
return useQuery({
key: () => ["users", route.params.user_id.toString(), "posts"],
query() {
console.log("user posts query executed");
return UserApi.getPosts(route.params.user_id.toString());
},
enabled: () => !!route.params.user_id
});
}); This example works well in I know that i can limit the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
The Where are you calling |
Beta Was this translation helpful? Give feedback.
-
I also want to participate in this discussion, since I cannot wrap my head around this use case.
To keep post query organized, we do export const usePost = defineQuery(() => {
const route = useRoute()
return useQuery({
key: () => ['posts', Number(route.params.postId)],
query: () => fetch(`/api/posts/${route.params.postId}`),
// only enable the query when there is a :postId param in the route
enabled: () => 'postId' in route.params,
});
}); This approach works fine for the first two views mentioned above. enabled: () => route.name === 'Post' || route.name === 'PostEdit', doesn't feel right 🤔 |
Beta Was this translation helpful? Give feedback.
Created #227 to track. Using
useQuery()
doesn't show this behavior, so this should be fixable