DamianOsipiuk / vue-query

Hooks for fetching, caching and updating asynchronous data in Vue
https://vue-query.vercel.app/
MIT License
1.11k stars 48 forks source link

useQueryProvider not working in <script setup> #254

Closed jens-morten-mikkelsen closed 2 years ago

jens-morten-mikkelsen commented 2 years ago

Hello, i recently tried using useQueryProvider in my setup, that can have multiple root compontent. It is using vue 2.7, and a normal Vue.use() installation doesn't work (for what ever reason, but this has its own seperate issue). After i switched one of my components over to useQueryProvider, im getting this error in the Browser:

vue.runtime.esm.js:4573 [Vue warn]: provide is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup().

(found in <Root>)

this is my component:

<template>
    <transition name="fade"
                mode="out-in"
                tag="div">
        <LoadingSpinner v-if="isLoading"
                        class="max-w-208 w-208"/>
        <section v-else
                 class="flex items-end space-x-24 sm:space-x-32">
            <p>
                show content
            </p>
        </section>
    </transition>
</template>

<script lang="ts" setup>
    import LoadingSpinner from "@/components/shared/LoadingSpinner.vue";
    import { useQueryProvider } from "vue-query";
    import {useCurrentBasket} from '@/core/api/basket/useBasketApi';
    import {watch} from 'vue';

    useQueryProvider();

    const props = defineProps<{
        deliveryDateChanges: number
    }>();

    const { refetch, isLoading } = useCurrentBasket();
    watch(() => props.deliveryDateChanges, () => refetch.value());
</script>
DamianOsipiuk commented 2 years ago

Hi, The error that you provided narrows it down to the exact same issue, why VueQueryPlugin approach does not work for you.

This is exactly the same approach as we are using to check if we can get the QueryClient from the context:

It appears that the application instance cannot be found. Why that happens i do not know, but it must be connected with the way you are instantiating Vue application.

jens-morten-mikkelsen commented 2 years ago

okay, thank you