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.

Automatically create and set Pinia instance as active when one does not exist

See original GitHub issue

What problem is this solving

  • Eliminates boilerplate createApp(...).use(createPinia()) (common usage) or setActivePinia(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 function defineStore({...}). Makes consumption easier where I can skip useStore() 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:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Maxim-Mazurokcommented, Apr 11, 2022

Not sure why setActivePinia(createPinia()) isn’t clean, but I’ve used it with my Custom Elements project, like so:

import { createPinia, setActivePinia } from "pinia";
import HelloWorld from "./components/HelloWorld";
import ByeWorld from "./components/ByeWorld";

setActivePinia(createPinia());

customElements.define("hello-world", HelloWorld);
customElements.define("bye-world", ByeWorld);

(since it’s elements project only, no app instance)

And it seems to work fine on a basic counter example.

1reaction
3zzycommented, Feb 20, 2022

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.

Read more comments on GitHub >

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

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