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'
69
+
const counterStore = useCounterStore()
70
+
</script>
71
+
```
72
+
73
+
<br/>
74
+
75
+
## Example
76
+
77
+
See [examples/vue-pinia/](https://github.com/vikejs/vike-vue/tree/main/examples/vue-pinia).
78
+
79
+
<br/>
80
+
65
81
## Populate store with `+data`
66
82
67
83
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
84
69
85
```ts
70
86
// pages/todos/+onData.ts
71
87
// Environment: server, client
88
+
72
89
export { onData }
73
90
74
91
importtype { PageContext } from'vike/types'
@@ -81,17 +98,17 @@ function onData(pageContext: PageContext & { data?: Data }) {
81
98
82
99
// Saving KBs: we don't need pageContext.data (we use the store instead)
83
100
// - 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
101
+
// - This optimization only works with SSR: if the page is pre-rendered then don't do this
85
102
deletepageContext.data
86
103
}
87
104
```
88
105
89
-
See To-Do List example at [examples/vue-pinia](https://github.com/vikejs/vike-vue/tree/main/examples/vue-pinia).
106
+
See complete To-Do List example at [examples/vue-pinia](https://github.com/vikejs/vike-vue/tree/main/examples/vue-pinia).
90
107
91
108
> [!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).
109
+
> 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
110
>
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.
111
+
> 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).
83
+
function minimize(movies) {
84
+
return movies.map((movie) => {
85
+
const { title, release_date, id } = movie
86
+
return { title, release_date, id }
87
+
})
88
+
}
89
+
</script>
90
+
```
83
91
84
92
<br/>
85
93
94
+
## Example
95
+
96
+
See [examples/vue-query/](https://github.com/vikejs/vike-vue/tree/main/examples/vue-query).
97
+
98
+
<br/>
86
99
87
100
## Settings
88
101
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