Skip to content

Commit 40828b6

Browse files
committed
docs: note about subscription flush
Close #2819
1 parent 128f435 commit 40828b6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

packages/docs/core-concepts/state.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ cartStore.$subscribe((mutation, state) => {
252252
})
253253
```
254254

255+
### Flush timing
256+
257+
Under the hood, `$subscribe()` uses Vue's `watch()` function. You can pass the same options as you would with `watch()`. This is useful when you want to immediately trigger subscriptions after **each** state change:
258+
259+
```ts{4}
260+
cartStore.$subscribe((state) => {
261+
// persist the whole state to the local storage whenever it changes
262+
localStorage.setItem('cart', JSON.stringify(state))
263+
}, { flush: 'sync' })
264+
```
265+
266+
### Detaching subscriptions
267+
255268
By default, _state subscriptions_ are bound to the component where they are added (if the store is inside a component's `setup()`). Meaning, they will be automatically removed when the component is unmounted. If you also want to keep them after the component is unmounted, pass `{ detached: true }` as the second argument to _detach_ the _state subscription_ from the current component:
256269

257270
```vue

0 commit comments

Comments
 (0)