🚨 Breaking Changes
This release completely changed how useInfiniteQuery() works, the parameters and returned values:
- The
mergeoption has been removed anddatacontains an object withpagesandpageParamsarrays that can be flattened. initialPagehas now been replaced withinitialPageParamloadMorehas been renamedloadNextPagegetNextPageParamis now a required option- Invalidating now works just as with regular queries, which means that you probably want to set
stalevalue higher (or disable it) to avoid refetching multiple pages when an infinite query is invalidated. Also, you might want to setrefetchOn*options tofalse. - It's now possible to have bi-directional navigation
- There is now
hasNextPageandhasPreviousPage
Any feedback on the feature and improvements is welcome!
Here is a complete example of what it looks in action:
<script setup lang="ts">
import { useInfiniteQuery } from '@pinia/colada'
import { onWatcherCleanup, useTemplateRef, watch } from 'vue'
const {
state: factsPages,
loadNextPage,
asyncStatus,
isDelaying,
hasNextPage,
} = useInfiniteQuery({
key: ['feed'],
query: async ({ pageParam }) => factsApi.get<CatFacts>({ query: { page: pageParam, limit: 10 } }),
initialPageParam: 1,
getNextPageParam(lastPage) {
return lastPage.next_page_url
}
// plugins
retry: 0,
delay: 0,
})
// we only need an array
const facts = computed(() => factPages.value.data?.pages.flat())
const loadMoreEl = useTemplateRef('load-more')
watch(loadMoreEl, (el) => {
if (el) {
const observer = new IntersectionObserver(
(entries) => {
if (entries[0]?.isIntersecting) {
loadNextPage()
}
},
{
rootMargin: '300px',
threshold: [0],
},
)
observer.observe(el)
onWatcherCleanup(() => {
observer.disconnect()
})
}
})
</script>
<template>
<div>
<button :disabled="asyncStatus === 'loading' || isDelaying" @click="loadMore()">
Load more (or scroll down)
</button>
<template v-if="facts?.length">
<p>We have loaded {{ facts.length }} facts</p>
<details>
<summary>Show raw</summary>
<pre>{{ facts }}</pre>
</details>
<blockquote v-for="fact in facts">
{{ fact }}
</blockquote>
<p v-if="hasNextPage" ref="load-more">Loading more...</p>
</template>
</div>
</template>🚀 Features
- Json-viewer for data, options, vars - by @posva (82c1b)
- Wip - by @posva (8cb00)
- Wip - by @posva (a5f3b)
- Wip infinite query - by @posva (3aca9)
- NextPage works - by @posva (d0110)
- ThrowOnError in infiniteQuery - by @posva (5010b)
- infinite:
🐞 Bug Fixes
- Use own local storage key - by @posva (feeec)
- Update mutations more often - by @posva (d2aec)
- Higher z-index for button - by @posva (2b8e5)
- infinite: Invalidation should refetch - by @posva (bc9c2)