pinia-plugin-persistedstate
pinia-plugin-persistedstate copied to clipboard
In the cookie store defineNuxtRouteMiddleware method was used in nuxt3 unable to get to the store of value
Describe the bug
In the cookie store defineNuxtRouteMiddleware method was used in nuxt3 unable to get to the store of value:
internationalStore: export const useInternationalStore = defineStore( 'international', () => { const currentLanguageCode = ref(null)
return {
currentLanguageCode
}
}, { persist: true } ) insomeMethod: const internationalStore = useInternationalStore() internationalStore.currentLanguageCode = 'en'
when I refresh the page middleware: const nuxtApp = useNuxtApp() const internationalStore = useInternationalStore(nuxtApp.$pinia) console.log(internationalStore.currentLanguageCode) // null
Reproduction
https://codesandbox.io/p/devbox/nuxt3-m4yfyk?file=%2Fnuxt.config.ts%3A6%2C1
System Info
chorome macox16
Used Package Manager
yarn
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guide.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a minimal reproducible of the bug.
You are, in fact, able to get the value, since you are getting null which is the initial value you gave it. What you are not doing is updating this value. Furthermore, you are not really supposed to directly assign a value to a ref(), you assign to its .value
property inside a setter.
export const useInternationalStore = defineStore('international',
() => {
const currentLanguageCode = ref(null);
function setLang(value: string) {
currentLanguageCode.value = value;
}
return { currentLanguageCode, setLang }
}, { persist: true }
)
in someMethod:
const internationalStore = useInternationalStore()
internationalStore.setLang('en')
See #221