You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { useCounterStore } from '../stores/useCounterStore'
65
+
const counterStore = useCounterStore()
66
+
</script>
67
+
```
68
+
69
+
<br/>
70
+
71
+
## Example
72
+
73
+
See [examples/vue-pinia/](https://github.com/vikejs/vike-vue/tree/main/examples/vue-pinia).
74
+
75
+
<br/>
76
+
65
77
## Populate store with `+data`
66
78
67
79
To populate your store with data fetched via the [`+data`](https://vike.dev/data) hook, use [`+onData`](https://vike.dev/onData) and [`pageContext.data`](https://vike.dev/pageContext#data).
68
80
69
81
```ts
70
82
// pages/todos/+onData.ts
71
83
// Environment: server, client
84
+
72
85
export { onData }
73
86
74
87
importtype { PageContext } from'vike/types'
@@ -81,17 +94,17 @@ function onData(pageContext: PageContext & { data?: Data }) {
81
94
82
95
// Saving KBs: we don't need pageContext.data (we use the store instead)
83
96
// - If we don't delete pageContext.data then Vike sends pageContext.data to the client-side
84
-
// - This optimization only works if the page is SSR'd: if the page is pre-rendered then don't do this
97
+
// - This optimization only works with SSR: if the page is pre-rendered then don't do this
85
98
deletepageContext.data
86
99
}
87
100
```
88
101
89
-
See To-Do List example at [examples/vue-pinia](https://github.com/vikejs/vike-vue/tree/main/examples/vue-pinia).
102
+
See complete To-Do List example at [examples/vue-pinia](https://github.com/vikejs/vike-vue/tree/main/examples/vue-pinia).
90
103
91
104
> [!NOTE]
92
-
> During [SSR](https://vike.dev/ssr), `+onData` is called only on the server. That's because the storestate is sent to the client, so that when the page hydrates, the client has the exact same state as the server — preventing [hydration mismatches](https://vike.dev/hydration-mismatch).
105
+
> During [SSR](https://vike.dev/ssr), `+onData` is called only on the server — the store's initial state (set by `initTodos()`) is automatically sent to the client, so you don't need to populate the store again on the client.
93
106
>
94
-
> As a result, the store doesn't need to be populated on the client: it's already populated on the server and then sent to the client.
107
+
> This approach prevents [hydration mismatches](https://vike.dev/hydration-mismatch), as it ensures the client has the exact same initial state as the server during SSR.
> The `vike-vue-query` extension requires [`vike-vue`](https://vike.dev/vike-vue).
80
+
function minimize(movies) {
81
+
return movies.map((movie) => {
82
+
const { title, release_date, id } = movie
83
+
return { title, release_date, id }
84
+
})
85
+
}
86
+
</script>
87
+
```
83
88
84
89
<br/>
85
90
91
+
## Example
92
+
93
+
See [examples/vue-query/](https://github.com/vikejs/vike-vue/tree/main/examples/vue-query).
94
+
95
+
<br/>
86
96
87
97
## Settings
88
98
89
-
The query client can receive custom options either by adding `queryClientConfig` to your `+config.ts` or adding a separate `+queryClientConfig.ts` file in your `pages` directory.
0 commit comments