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

Stores cannot be used before lifecycle hooks #255

Closed
LadislavBohm opened this issue Oct 10, 2020 · 6 comments
Closed

Stores cannot be used before lifecycle hooks #255

LadislavBohm opened this issue Oct 10, 2020 · 6 comments
Labels
v2 issues related to v2 for Vue 3

Comments

@LadislavBohm
Copy link

Reproduction

Maybe it is my misunderstanding of how stores work and it's by design but currently Vue throws a warning when I use lifecycle hooks and declare them after declaring a store. I would like to declare stores at the beginning of setup() method and then use them within my hooks.

I modified the sandbox to include the warning, if you move the lifecycle hook at the top of setup method it goes away.
https://codesandbox.io/s/pinia-vue-3-demo-forked-85iy9?file=/src/App.vue

Warning thrown:
onBeforeMount is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.

Is this behavior ok? If so, am I supposed to instantiate store every time inside hooks?

Expected behavior

I would expect using stores with hooks to be without any restrictions.

Actual behavior

I have to declare lifecycle hooks before declaring stores as if they were await calls.

@posva
Copy link
Member

posva commented Oct 10, 2020

Yeah, this is a limitation that needs to wait for vuejs/rfcs#212. For now, it's necessary to call the useStore function at the end of setup or call pinia.store(useStore) for each store in your main.js file (it will also remvoe the code splitting)

@LadislavBohm
Copy link
Author

LadislavBohm commented Oct 19, 2020

I am wondering whether there is an easier way to call pinia.store for all stores. Currently this feels like the biggest issue for me as moving things around setup function does not make it readable or maintainable. Also not always possible and required multiple instances of pinia stores around one setup function.

In some compiled language I would use reflection to register stuff like that within an assembly. Doesn't seem like the mentioned RFC got a lot of positive feedback 😰

@posva
Copy link
Member

posva commented Oct 19, 2020

It's annoying for me too. The easiest workaround is to import all stores in your main.js file and call pinia.store(useStore) to instantiate them.

The RFC will go through a way or another because there is a need for it

@LadislavBohm
Copy link
Author

Thanks I will end up doing that. Generally is this the same reason why this is happening?

const firstInstance = getCurrentInstance(); //resolves properly
const userEditStore = useSomeStore();
const secondInstance = getCurrentInstance(); //always null

This seems to be the reason why useVuelidate which is internally calling getCurrentInstance is failing. I wanted to bind vuelidate directly to my store state so I will just import all stores at start like you mentioned.

@posva
Copy link
Member

posva commented Oct 19, 2020

yes, it breaks current instance because it creates one itself to outlive component scoped for the state, but creating it outside of a component, by calling useStore for the first time, doesn't break any current instance 🙂

@posva posva closed this as completed in 483335c Dec 31, 2020
@posva posva added the v2 issues related to v2 for Vue 3 label Feb 27, 2021
@lk0606
Copy link

lk0606 commented Apr 24, 2022

@posva

I use pinia^2.0.13 in vue2, and postinstall npx vue-demi-switch 2, alll ok.
but when i use onBeforeMount or others Lifecycle in setup, how can i resolve it?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v2 issues related to v2 for Vue 3
Projects
None yet
Development

No branches or pull requests

3 participants