Skip to content

Subscribe example #443

Closed Answered by posva
husayt asked this question in Help and Questions
Apr 17, 2021 · 2 comments · 4 replies
Discussion options

You must be logged in to vote

At the end $subscribe is just a watcher, so the best is to use a watch on the pinia state

const pinia = createPinia()
watch(pinia.state, handler, { deep: true })

The handler would write to local storage.

It's also at the handler level that one should check for specific stores' state as they are created on the fly as they get used. This means that when the watcher is created, it's observing an empty object (pinia.state.value). You could still watch a specific store with

// given defineStore({ id: 'myStore' })
watch(() => pinia.state.value.myStore, handler, { deep: true })

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@husayt
Comment options

@kassuro
Comment options

@husayt
Comment options

@posva
Comment options

Answer selected by husayt
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants