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

[6.x] Switch from Vuex to Pinia #11446

Merged
merged 12 commits into from
Feb 12, 2025
Merged

[6.x] Switch from Vuex to Pinia #11446

merged 12 commits into from
Feb 12, 2025

Conversation

jasonvarga
Copy link
Member

@jasonvarga jasonvarga commented Feb 12, 2025

This switches Vuex to Pinia.

Vuex has always been a little confusing and overkill with its actions + mutations workflow. Now using Pinia it's much simpler.

Vuex was being used in a number of places in order to keep chunks of centralized data. In most places I've refactored away from using Vuex or Pinia at all. Instead, opting to use Vue's ref to keep a simple reactive array of stuff.

The only real place that uses Pinia now is the publish component (which is used all over the place).

There are some simple breaking changes though.


The this.$store Vuex helper has been removed. Any components attempting to access the store were probably already getting the injected storeName property. Now the actual Pinia store itself will be provided. There's no need for the storeName property anymore if you were just using it to access the store.

{
    inject: [
-       'storeName',
+       'store',
    ],
    methods: {
        myMethod() {
-           const values = this.$store.state.publish[this.storeName].values;
+           const values = this.store.values;
        }
    }
}

If you were using Statamic.$store, you should also be relying on the provided store.


Similarly, in field actions you would get the store api and storeName in the payload. Now you get the store itself.

Statamic.$fieldActions.add('text-fieldtype', {
    title: 'Example',
    run: ({ store, storeName }) => {
-       const values = store.state.publish[storeName].values;
+       const values = store.values;
    }
})

If you were dispatching actions or committing mutations, you will now call methods on the store directly.

-this.$store.dispatch(`publish/${this.storeName}/doSomething`), arg);
+this.store.doSomething(arg);

-this.$store.commit(`publish/${this.storeName}/doSomething`), arg);
+this.store.doSomething(arg);

If you want to create your own store, you can now use Pinia yourself.

-Statamic.$store.registerModule('myModule', {
-  state: {
-    hello: 'world',
-  }
-});
-const hello = Statamic.$store.state.myModule.hello;

+const useStore = Statamic.$pinia.defineStore({
+  state: {
+    hello: 'world'
+  }
+})
+const store = useStore();
+const hello = store.hello;

Contrary to the Pinia docs, you should not npm require pinia, and not import { whatever } from 'pinia'.
Instead, use methods from Statamic.$pinia which ensures you are sharing the same instance of Pinia with Statamic.

@jasonvarga jasonvarga marked this pull request as ready for review February 12, 2025 19:51
@jasonvarga jasonvarga merged commit 30ed099 into master Feb 12, 2025
19 checks passed
@jasonvarga jasonvarga deleted the pinia branch February 12, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant