Kong / swrv

Stale-while-revalidate data fetching for Vue

Home Page:https://docs-swrv.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Could not get current instance, check to make sure that `useSwrv` is declared in the top level of the setup function.

hitachigjoafli opened this issue · comments

Hello im trying to use Swrv on my vue 2.6.14 app but i get the following error message "Could not get current instance, check to make sure that useSwrv is declared in the top level of the setup function.". I ues nuxt also.

I have the following in my package.json:

    "swrv": "^0.9.6",
 "@vue/composition-api": "^1.7.1",
    "nuxt": "^2.15.8",

And my code is the following:

<template>
  <div>
    <div>
      <div v-if="error">{{ error }}</div>
      <div v-if="data === undefined && !error">Loading...</div>
      <div v-if="data">
        <div>{{ data.body }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import useSWRV from 'swrv'

const fetcher = (key) =>
  fetch(`http://localhost:3001/${key}`).then((res) => res.json())

export default {
  name: 'LoginPage',
  setup(props, context) {
    const { data, error } = useSWRV('ticket', fetcher)

    return {
      data,
      error,
    }
  },
}
</script>

I don't really see anything here that would cause the error. I'd need a minimal reproduction via CodeSandbox or something in order to help debug.

For starters, you don't have your plugins/vue-composition-api.js file registered as a plugin in the nuxt.config.js file; however, I still see the same error regardless.

If I had to guess, it's something to do with the versions that are being resolved in your project. If you look at swrv/examples/ssr-nuxt/ on this commit (0.9.6 release), everything is working as expected, so I'm guessing there's something going on with the specific dependency versions in your sandbox, or something else in the setup.