Automatically create and set Pinia instance as active when one does not exist
See original GitHub issueWhat problem is this solving
- Eliminates boilerplate
createApp(...).use(createPinia())
(common usage) orsetActivePinia(createPinia())
- Conceptually decouples Vue App instance from Pinia stores. You can use Pinia stores at any point… before or after Vue app creation.
- Faster bootstrapping for common usage pattern. If I am not mistaken, while more than one pinia instance can be instantiated, one pinia instance suffices and is the common usage pattern that is recommended.
- Eliminates the chicken and egg problems with up-front logic dependent on pinia stores that is ran prior to Vue App creation. (In my case, I have authentication-logic running in my main entry file before any other logic.)
- Allows for pattern where you can export single store instance using
defineStore({...})()
without worries opposed to useStore functiondefineStore({...})
. Makes consumption easier where I can skipuseStore()
assignment to a local variable when I need to use the store instance in multiple places. - Author does not have to deal with
getActivePinia was called with no active Pinia. Did you forget to install pinia?
issues again 😃
Proposed solution
As part of defineStore options (3rd parameter), introduce some new setting (e.g. createActivePinia
) which will create a Pinia instance (and set it active) if it does not exist at the time the store instance is created/retrieved via useStore()
. Default setting to true.
Describe alternatives you’ve considered
- Same as proposed solution but default
createActivePinia
setting to false.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Automatically create and set Pinia instance as active ... - GitHub
What problem is this solving Eliminates boilerplate createApp(...).use(createPinia()) (common usage) or setActivePinia(createPinia()) ...
Read more >Using a store outside of a component - Pinia
Behind the scenes, useStore() injects the pinia instance you gave to your app . This means that if the pinia instance cannot be...
Read more >Testing stores - Pinia
This object will be used by the testing pinia to patch stores when they are created. Let's say you want to initialize the...
Read more >Plugins - Pinia
Plugins are added to the pinia instance with pinia.use() . The simplest example is adding a static property to all stores by returning...
Read more >Getters - Pinia
Getters are exactly the equivalent of computed values for the state of a Store. ... getters: { // type is automatically inferred because...
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 FreeTop 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
Top GitHub Comments
Not sure why
setActivePinia(createPinia())
isn’t clean, but I’ve used it with my Custom Elements project, like so:(since it’s elements project only, no app instance)
And it seems to work fine on a basic counter example.
Yes, something like this would also be useful when Vue components are consumed as Custom Elements and there’s no Vue instance available to install the plugin. See this thread for context.