#What is the best way to use pinia with useAsyncData

1 messages · Page 1 of 1 (latest)

deft coral
#

I have a very simple component like this:


<script setup lang="ts">
const { fetchNotifications, notifications } = useNotificationsStore();
const { status, refresh } = await useLazyAsyncData(async () => fetchNotifications("DEFAULT").then(() => true));
</script>

<template>
    <p v-if="status === 'success'">
        <span v-for="(notif, i) in notifications" :key="i">
            {{ notif.context }}
        </span>
    </p>
</template>


but it always causes Hydration completed but contains mismatches.
what is the best way to get this data

azure sandal
#

Can you show the code in useNotificationsStore?

deft coral
# azure sandal Can you show the code in `useNotificationsStore`?

sure



export const useNotificationsStore = defineStore({
    id: "NotificationsStore",
    state: () => ({
        notifications: [] as NotificationType[],
    }),
    actions: {
        async fetchNotifications(category: string, options: { skip: number; limit: number }) {
            try {
                const res = await useCustomFetch(`...API URL`, { API_URL: "notification" });
                this.notifications = res.data;
            }
            catch (e: any) {
                throw new Error(e.message);
            }
        },
    },

    getters: {
        hasUnreadNotifications(state) {
            return state.notifications.some(notification => !notification.is_read);
        },
    },
});


azure sandal