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

In the cookie store defineNuxtRouteMiddleware method was used in nuxt3 unable to get to the store of value #283

Open
4 tasks done
pocketChao opened this issue Feb 29, 2024 · 3 comments
Labels
🔍️ pending triage This issue needs to be looked into

Comments

@pocketChao
Copy link

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

@pocketChao pocketChao added the 🔍️ pending triage This issue needs to be looked into label Feb 29, 2024
@sanjacob
Copy link

sanjacob commented Mar 4, 2024

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.

@sanjacob
Copy link

sanjacob commented Mar 4, 2024

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')

@sanjacob
Copy link

sanjacob commented Mar 4, 2024

See #221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔍️ pending triage This issue needs to be looked into
Projects
None yet
Development

No branches or pull requests

2 participants