Compose data in vue by using useQuery
data in a Pinia store?
#6798
-
Hey all! I'm trying to add a layer (in the form of a Pinia store) between the I understand using // api/todos.ts
// Wrapping query in a composable...
// Maybe I could put some computed's in here, but I'd like to keep this data layer separate from page logic
export function useTodos() {
const { data: todos } = useQuery({ queryKey: ["todos"], queryFn: fetchTodos });
return { todos };
} And the page store: // stores/page.ts
export const usePageStore = defineStore("page", () => {
const { todos } = useTodos();
const todosByUserId = computed(() => {
return _.groupBy(todos.value, "userId");
});
// lots of other getters...
// lots of other actions...
return {
todosByUserId,
// ...
}
}); Then, my page is a composite of a bunch of components that each use different parts (some overlap) of the store: <!-- Page.vue -->
<template>
<TodoMenuBar /> <!-- Uses entirety of the "todos" data to show options/filters -->
<TodoList /> <!-- Uses entirety of the "todos" data to show a list -->
<TodoDetailsSideBar /> <!-- Can show a single todo item -->
<!-- More components... -->
</template> The problem is, the |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hi! Can you provide a minimal reproduction for this problem? |
Beta Was this translation helpful? Give feedback.
-
Do You find solution? I have same problem |
Beta Was this translation helpful? Give feedback.
-
I think your code is related to reactivity and lifecycle hooks in Vue and Pinia. Also api/todos.ts
stores/page.ts
Page.vue
TodoMenuBar.vue
|
Beta Was this translation helpful? Give feedback.
-
Sorry, forgot about this thread. I did end up figuring out the issue. Here's a min example if anyone is stuck on this. (Not sure what the comment above me is trying to say. I wouldn't recommend using |
Beta Was this translation helpful? Give feedback.
Sorry, forgot about this thread. I did end up figuring out the issue. Here's a min example if anyone is stuck on this.
(Not sure what the comment above me is trying to say. I wouldn't recommend using
watchEffect
to manage values like they are doing. Passing around refs is just fine.)