I'm used to using Pinia/Vuex for dispatching axios requests and storing the data in the store, then using the getters for computational operations on the data. I'm using Vue 3.
A simple example: let's say I have a table, I fetch the data from the action and put it in the state, then use different getters for different sorting filters
Now, vue-query docs say that there might be no need for Vuex/Pinia/state management when using vue-query. This is where my problem comes in, how to organise the code so I can still use the computed properties/getters without having them locally in the file where I'm using vue-query?
I'm aware I might be totally missing the point of vue-query. In that case, please list some resources where I can learn more about it (that's not just the docs)..
I tried fetching the data from the store (using the action in the store as the query function), then relying on vue-query properties isLoading
, isError
in the template.
After that, I used the getters from the store.
This feels wrong, duplicated data in vue-query and Pinia??
Here's the minimal example:
<script setup lang="ts">
const { isLoading, isError, error } = useQuery({
queryKey: ["items"],
queryFn: store.fetchItems,
});
const {
listItems: items,
} = storeToRefs(store);
</script>
<template>
<template v-if="isLoading"> loading </template>
<template v-else-if="isError">
{{ error }}
</template>
<template v-else>
<div>{{ items }}</div>
</template>
</template>
store has:
const list: Ref<Item[]> = ref([]);
const listItems = computed(() => list.value);
async function fetchItems(): Promise<Item[]> {
try {
const response = await axios.get<Item[]>(`${dbUrl}/items`);
list.value = response.data || [];
return list.value;
} catch (err) {
list.value = [];
throw new Error(err as string);
}
}
return {
fetchItems,
listItems,
}