question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 <project-name> 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>

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:13
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Schetnancommented, Jul 15, 2022

@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.

1reaction
Schetnancommented, Jul 13, 2022

@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>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found