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
*`fetch-only` means that the query will only fetch the data from the adapter plugins. The data will be stored in the cache when the query is resolved.
167
177
*`cache-only` means that the query will only fetch the data from the cache.
168
178
*`no-cache` means that the query will not use the cache and only fetch the data from the adapter plugins. No data will be stored in the cache.
179
+
180
+
## Items list
181
+
182
+
Usually we write list item components that are used to display a list of items. It is recommended to only pass the key to the list item component and then use `queryFirst` to fetch the data in the list item component. By default the fetch policy is `cache-first`, so the data will be red from the cache in the item component and no unnecessary requests will be made for each items.
183
+
184
+
The benefits are that the data is co-located with the component that uses it and there is no need to specify the types of the item prop again (usually a simple `id: string | number` is enough). The item component is also easier to potentially reuse in totally different contexts.
185
+
186
+
::: code-group
187
+
188
+
```vue [TodoList.vue]
189
+
<script lang="ts" setup>
190
+
const store = useStore()
191
+
const { data: todos } = await store.Todo.queryMany()
192
+
</script>
193
+
194
+
<template>
195
+
<!-- Only pass the id here -->
196
+
<TodoItem
197
+
v-for="{ id } in todos"
198
+
:id
199
+
:key="id"
200
+
/>
201
+
</template>
202
+
```
203
+
204
+
```vue [TodoItem.vue]
205
+
<script lang="ts" setup>
206
+
const props = defineProps<{
207
+
// Easy to type prop
208
+
id: string
209
+
}>()
210
+
211
+
const store = useStore()
212
+
// No additional request is made here, the data is read from the cache
213
+
const { data: todo } = await store.Todo.queryFirst(props.id)
214
+
// Enjoy inferred types here too!
215
+
</script>
216
+
```
217
+
218
+
:::
219
+
220
+
## Co-locating queries
221
+
222
+
The best of both worlds! You can co-locate the queries with the components that use them, while still enjoying the benefits of a centralized store thanks to the cache -- letting it deduplicating and synchronizing all components. This is a powerful pattern that improves the independence of components and thus their maintainability.
223
+
224
+
::: code-group
225
+
226
+
```vue [AppHeader.vue]
227
+
<script lang="ts" setup>
228
+
const store = useStore()
229
+
// The query is co-located with the component, not in a store somewhere else
230
+
const { data: user } = await store.User.queryFirst('user-id')
231
+
</script>
232
+
233
+
<template>
234
+
<div>
235
+
<h1>{{ user.name }}</h1>
236
+
</div>
237
+
</template>
238
+
```
239
+
240
+
```vue [UserProfile.vue]
241
+
<script lang="ts" setup>
242
+
const store = useStore()
243
+
// The centralized cache ensures that the data is up to date here too
244
+
const { data: user } = await store.User.queryFirst('user-id')
Copy file name to clipboardExpand all lines: docs/guide/learn-more.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,12 +46,13 @@ The reactive normalized cache in rstore is a key feature that ensures your appli
46
46
47
47
The cache is also reactive, meaning reading from the cache in a `computed` will always keep the components updated. In fact, each time you use `store.Todo.queryMany()` you get a computed ref that reads from the cache.
48
48
49
-
::: details
50
-
51
49
The reactive normalized cache in rstore offers several benefits.
52
50
- Consistency is maintained by normalizing the data, ensuring a single source of truth for each item, which prevents duplication and inconsistencies.
53
51
- Reactivity is another advantage, as the cache automatically updates any part of your application that depends on the data whenever changes occur, keeping your UI in sync with the latest state.
54
52
- Efficiency is achieved through the structured format of normalized data, allowing for quicker and less overhead-intensive queries and updates.
53
+
- Co-locating the data requirements with the components that use them is a powerful pattern. By using the `queryMany` and `queryFirst` composables, you can easily fetch and display data in your components without worrying about deduplicating or synchronizing with other components.
0 commit comments