Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

using useContext() causes error "Error: This must be called within a setup function." #643

Closed
Schetnan opened this issue Jul 2, 2022 · 12 comments · Fixed by #645
Closed
Assignees
Labels
bug Something isn't working

Comments

@Schetnan
Copy link

Schetnan commented Jul 2, 2022

Operating system: OS/X
Node-version: 14.18.2
npm version: 6.14.15

Using useContext in page setup function causes:

"Error: This must be called within a setup function."

1). Create new project via: npx create-nuxt-app
2). npm install @nuxtjs/composition-api --save
3). Add to buildModules in nuxt.config.js
{
buildModules: [
'@nuxtjs/composition-api/module'
]
}

  1. Update pages/index.vue to the following:
<template>
  <Tutorial/>
</template>

<script>
import { defineComponent, useContext } from '@nuxtjs/composition-api'
export default defineComponent({
  name: 'IndexPage',

  setup() {
    const nuxtContext = useContext()
  }
})
</script>
@Schetnan Schetnan added the bug Something isn't working label Jul 2, 2022
danielroe added a commit that referenced this issue Jul 8, 2022
BREAKING CHANGE: Some of the API and behaviour differ between Vue 2.7 and `@vue/composition-api`. In addition, there are some composition utilites that are no longer exported from `@nuxtjs/composition-api`, such as `defineAsyncComponent`, `warn`, etc.

Please read https://blog.vuejs.org/posts/vue-2-7-naruto.html for the announcement and more information.

resolves #644, resolves #643, resolves #593, resolves #19
xicri added a commit to xicri/genshin-dictionary that referenced this issue Jul 9, 2022
@Schetnan
Copy link
Author

@danielroe When will this fix be available in a general 2.5.x release?

@danielroe
Copy link
Member

What do you mean?

@Schetnan
Copy link
Author

@danielroe We have lots of projects that now break if we delete our package-lock.json and try to rebuild. ( Due to this issue ). I need to know what versions to point our package.json to so that we will pick up the fix - that closed this issue.

@danielroe
Copy link
Member

This fix is available in "^0.33.0".

@Schetnan
Copy link
Author

Schetnan commented Jul 13, 2022

@danielroe How do I fix this for the older 2.5.x versions?

@danielroe
Copy link
Member

I'm not really sure what your problem is. Maybe open a new issue with a minimal reproduction?

@Schetnan
Copy link
Author

@danielroe I think my origin report should work. Just going to the Nuxt 2 site (docs ) and create a new project as I have highlighted above, installing the @nuxtjs/composition-api, then adding the following in the setup. This problem is in existing nuxt 2 projects - not Nuxt 3 or the bridge.

<template>
  <Tutorial/>
</template>

<script>
import { defineComponent, useContext } from '@nuxtjs/composition-api'
export default defineComponent({
  name: 'IndexPage',

  setup() {
    const nuxtContext = useContext()
  }
})
</script>

@danielroe
Copy link
Member

It works fine: https://stackblitz.com/edit/github-k8mxkn

@Schetnan
Copy link
Author

@danielroe @xicri
I recreated a clean project with npx create-nuxt-app <project-name>. I then added the latest version of the nuxt composition api and the project worked.

I was able to get one of my other projects working by copying the dependencies from the fresh project over to the older one.

Thanks for sticking with me.

xicri added a commit to xicri/genshin-dictionary that referenced this issue Jul 18, 2022
xicri added a commit to xicri/genshin-dictionary that referenced this issue Jul 23, 2022
xicri added a commit to xicri/genshin-dictionary that referenced this issue Jul 23, 2022
@sppmstar
Copy link

sppmstar commented Mar 22, 2023

I am still experiencing that issue with composition-api v 0.33.1 :(

I tested with a blank project and that works fine... but not in my existing project. This is a production app, so i cannot just start from scratch...

@vuthanhbayit
Copy link

I am still experiencing that issue with composition-api v 0.33.1 :(

I tested with a blank project and that works fine... but not in my existing project. This is a production app, so i cannot just start from scratch...

I have the same issue

@jeanmatthieud
Copy link

I had the same issue, but was due to the use of "useContext()" in a pinia store.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants