github posva/pinia-colada v0.20.0

latest releases: @pinia/colada-devtools@0.4.0, @pinia/colada-plugin-retry@0.2.0, @pinia/colada-nuxt@0.3.0...
7 hours ago

   🚨 Breaking Changes

This release completely changed how useInfiniteQuery() works, the parameters and returned values:

  • The merge option has been removed and data contains an object with pages and pageParams arrays that can be flattened.
  • initialPage has now been replaced with initialPageParam
  • loadMore has been renamed loadNextPage
  • getNextPageParam is now a required option
  • Invalidating now works just as with regular queries, which means that you probably want to set stale value higher (or disable it) to avoid refetching multiple pages when an infinite query is invalidated. Also, you might want to set refetchOn* options to false.
  • It's now possible to have bi-directional navigation
  • There is now hasNextPage and hasPreviousPage

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

   🐞 Bug Fixes

    View changes on GitHub

Don't miss a new pinia-colada release

NewReleases is sending notifications on new releases.