-
-
Notifications
You must be signed in to change notification settings - Fork 44
useInfiniteQuery: changing the key should create a new cache entry #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can there also be an issue with import { useQuery, type EntryKey } from '@pinia/colada'
import { useIntersectionObserver } from '@vueuse/core'
import { ref, computed, watch, type MaybeRefOrGetter } from 'vue'
export const useInifiniteQuery = ({
key,
query,
loadMoreButton,
dataKey,
page,
}: {
key: MaybeRefOrGetter<EntryKey>
query: () => Promise<any>
loadMoreButton: Ref<HTMLElement | null>
dataKey: string
page: Ref<number>
}) => {
const data = ref<any[]>([])
const { state, asyncStatus } = useQuery({
key,
query,
})
const hasMore = computed(() => state.value?.data?.hasMore ?? false)
function loadMore() {
console.log('loadMore', hasMore.value, page.value)
if (hasMore.value) {
page.value++
}
}
watch(
state,
(newData) => {
console.log('newData', newData.data[dataKey])
if (page.value === 1) {
data.value = newData.data[dataKey] ?? []
} else {
data.value = [...data.value, ...(newData.data[dataKey] ?? [])]
}
},
{ immediate: true }
)
useIntersectionObserver(loadMoreButton, ([entry]) => {
if (
entry?.isIntersecting &&
hasMore.value &&
asyncStatus.value !== 'loading'
) {
loadMore()
}
})
return {
state,
data,
hasMore,
loadMore,
asyncStatus,
}
} |
Hi! I'm having the same stated problem, which sadly makes this feature unusable for me at this moment, do we know anything about any advancement on it? Is the plan to first resolve the API for PS: Thank you so much for all your work @posva! |
@r-moret, I made a quick patch you can use. Froze the pinia-colada version to https://gist.github.com/VianneyRousset/f11fa3afeedf894a0d97a9f3d543651f |
@r-moret It just needs to be fixed (#178 (comment)). I still haven't had the time for it so feel free to give it a try! |
Uh oh!
There was an error while loading. Please reload this page.
Changing the key should create a new cache entry that cumulates new pages.
Modifying the
queryParam
does reactively setdata
to the cached value or triggerquery()
andmerge()
in case of cache-miss. However, as thepages
variable is shared between allmerge
calls, queried data from all queryParams are mixed.Here is a small playground to illustrate the issue. Try triggering few
loadMore
withqueryParam = 'a'
and switch it to'b'
and load some extra data.Ideally, the
query
function would have access to the current cached data (e.g. in thecontext
argument). Thus, themerge
function would receive the accumulated pages of the currentkey
.Originally posted by @VianneyRousset in #178
The text was updated successfully, but these errors were encountered: