Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Nuxt 2 --> 3 #2766

Merged
merged 3 commits into from
Nov 28, 2024
Merged

docs: Nuxt 2 --> 3 #2766

merged 3 commits into from
Nov 28, 2024

Conversation

david-mears-2
Copy link
Contributor

Replace tip that was relevant to Nuxt 2 with the equivalent syntax for Nuxt 3.

I'm not sure whether this is in fact the intended usage pattern / syntax for Nuxt 3, but I think what I've written is an improvement over the status quo, since at least it works.

If this PR results in, or is deemed as, a definitive answer to this question, it may resolve that discussion.

Replace tip that was relevant to Nuxt 2 with the equivalent syntax for Nuxt 3.
Copy link

netlify bot commented Sep 5, 2024

Deploy Preview for pinia-official ready!

Name Link
🔨 Latest commit 1847d7d
🔍 Latest deploy log https://app.netlify.com/sites/pinia-official/deploys/6748747d89db2c0008212f44
😎 Deploy Preview https://deploy-preview-2766--pinia-official.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Sep 5, 2024

Deploy Preview for pinia-playground canceled.

Name Link
🔨 Latest commit 1847d7d
🔍 Latest deploy log https://app.netlify.com/sites/pinia-playground/deploys/6748747d6787b200083d090e

@david-mears-2 david-mears-2 changed the title Update nuxt.md Docs: Nuxt 2 --> 3 Sep 5, 2024
Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


```js
import { useStore } from '~/stores/myStore'
const store = useStore(useNuxtApp().$pinia);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still problematic because of teh usage of useNuxtApp(). So maybe, adapting the example to something else where the context is not available and this is needed, is better. Or maybe, specify that the example below is Nuxt 2

Copy link
Contributor Author

@david-mears-2 david-mears-2 Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the whole of Nuxt 3 is a place “where the context is not available”, until you call useNuxtApp(), which is the Nuxt 3 equivalent of context https://nuxt.com/docs/guide/going-further/nuxt-app (eta I fixed that link)
So I think whenever a pinia store is used outside of a setup, it’s necessary to have passed it the pinia instance found on the Nuxt app instance - right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe this is just not longer needed, only in some more convoluted examples like lazy invocation in a plugin

function nuxtPlugin() {
  const nuxt = useNuxtApp()
  function something() {
    useStore(nuxt.$pinia)
  }

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there someone we can ask? It seems important to clarify, since I read elsewhere in the documentation that

you will have to pass the pinia instance to useStore(). This prevents pinia from sharing global state between different application instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existence of the tip implies to me that the module isn’t automatically passing the pinia instance to the store, except in the setup function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there someone we can ask?

I think you are already asking the right person 😅

These parts of the docs could be updated. The different is a little bit more subtle, as it works now inside navigation guards and other parts of the application since app.runWithContext(). The nuxt function also follows this rule BTW. Feel free to give this a try if you want

Copy link
Contributor Author

@david-mears-2 david-mears-2 Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'm just very confused about all of this. The existing documentation made me believe I needed to do something to prevent Cross-Request State Pollution when using a pinia store inside an onMounted callback. I'm not sure if you are saying that's true or false, or how one can do it in Nuxt 3 (given you say the use of useNuxtApp() is problematic).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't call useStore() within onMounted, just call it outside, and consume the store within onMounted().
You cannot useNuxtApp() anywhere (as stated in the docs), only in places where there is Vue injection context (a rather implicit concept), in other words, within setup and composables and other explictely mentioned places (because they are injection context aware) like stores, middleware, plugins, etc

Copy link
Contributor Author

@david-mears-2 david-mears-2 Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't call useStore() within onMounted, just call it outside, and consume the store within onMounted().

That is what my example / diff does. I call useStore (and useNuxtApp) in the setup function, and store.doAction() in the onMounted().

I don't believe I have enough context about Nuxt and Vue to be able to help complete this documentation, but I thank you for your kind responses.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you don't need to pass the pinia instance within setup. I assumed the code sample was outside of setup() and that onMounted() was a mistake there. I will rework the example when I can but anybody should feel free to give it a try

packages/docs/ssr/nuxt.md Outdated Show resolved Hide resolved
@posva posva merged commit 3223983 into vuejs:v2 Nov 28, 2024
3 of 4 checks passed
@posva posva changed the title Docs: Nuxt 2 --> 3 docs: Nuxt 2 --> 3 Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

2 participants