Skip to content

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

@bitx1

Description

@bitx1

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions