I'm using Pinia for state managment, and I want the state to persist when the page is refeshed.
I'm aware of two options:
Use a plugin. Vuex has a vuex-persistedstate plugin for this, and Pinia has a similar plugin but it's still under development.
Use local storage. Luckily Quasar has a LocalStorage plugin which would be nice to use here. But I'm not sure how to integrate it with Pinia, thus the reason for this post.
I found a nice tutorial doing something similar with Pinia + Vueuse.
And I tried adapting it to my needs with Pinia + TypeScript + Quasar LocalStorage Plugin as per below:
import { User } from 'firebase/auth';
import { defineStore } from 'pinia';
import { LocalStorage } from 'quasar';
type CreateMutable<T> = { -readonly [P in keyof T]: CreateMutable<T[P]> };
export const useUserStore = defineStore('user', {
state: () => ({
// user: {} as CreateMutable<User>, // This was my previous non-persistent state
user: LocalStorage.set('user', {}) as unknown as CreateMutable<User>, // This is my attempt at using LocalStorage persistent state
}),
actions: {
setUser(userData: User) {
this.user = userData;
},
setPhotoURL(photoURLData: string | null) {
this.user.photoURL = photoURLData;
},
},
});
Unfortunately it doesn't work.
The state does not persist on page refresh.
Any ideas on how to make this work?