Best practices for state/composable to depend on fetched data?
I have a useUser composable that fetches API data to build the user: userInfo and companyInfo.
I get the error that a composable is used outside the Nuxt instance.
I want to know the best practices for implementing something like this, if anyone knows enough to help
Here's the code for the user composable and the user plugin.
composables/useUser.js
plugins/user-plugin.js
4 Replies
Do not await on top level in your composables.
https://nuxt.com/docs/api/composables/use-async-data
https://nuxt.com/docs/guide/going-further/nuxt-app#the-nuxt-context
https://nuxt.com/docs/getting-started/data-fetching
Hey @Mähh thanks a lot! I love the simplicity of the suggestion!
I still have the same error I had before.
Seems linked to the use of useAsyncData in the composable (from the stack)
from docs, nuxtPlugin should be in the nuxt context.
useAsync is only available in such contexts.
any reason why you do it in a plugin, and not e.g. in a middleware?
Anyways, you can give
nuxtApp.runWithContext
a try https://nuxt.com/docs/api/composables/use-nuxt-app#runwithcontextI still have the problem that running useAsyncData in a composable returns a 500
And I can't, for the life of me, understand how to use runWithContext