using useContext() causes error "Error: This must be called within a setup function."
See original GitHub issueOperating 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 <project-name> 2). npm install @nuxtjs/composition-api --save 3). Add to buildModules in nuxt.config.js { buildModules: [ ‘@nuxtjs/composition-api/module’ ] }
- 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>
Issue Analytics
- State:
- Created a year ago
- Reactions:13
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Nuxt, VueJS This must be called within a setup function
I need to implement some changes. When I download a project from gitlab and run it throws me an error:This must be called...
Read more >Nuxt, VueJS This must be called within a setup function-nuxt.js
From the look of things, it seems you. You created another plugin for the VueCompositionAPI when it is installed already e.g @nuxtjs/composition-api.
Read more >useHooks - Easy to understand React Hook recipes
We bring you easy to understand React Hook code recipes so you can learn how React hooks work and feel more comfortable writing...
Read more >The Guide to Learning React Hooks (Examples & Tutorials)
Learn all about React Hooks with this hands-on guide. Includes tutorials and code examples on using hooks for state and effects, for context...
Read more >unctx - npm
Start using unctx in your project by running `npm i unctx`. ... { // This is similar to vue setup function // Any...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
@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.