--- id: infinite-queries title: Infinite Queries ref: docs/framework/react/guides/infinite-queries.md --- [//]: # 'Example' ```tsx import { Switch, Match, For, Show } from 'solid-js' import { useInfiniteQuery } from '@tanstack/solid-query' function Projects() { const fetchProjects = async ({ pageParam }) => { const res = await fetch('/api/projects?cursor=' + pageParam) return res.json() } const projectsQuery = useInfiniteQuery(() => ({ queryKey: ['projects'], queryFn: fetchProjects, initialPageParam: 0, getNextPageParam: (lastPage, pages) => lastPage.nextCursor, })) return ( Loading... Error: {projectsQuery.error.message} {(group) => ( {(project) => {project.name}} )} projectsQuery.fetchNextPage()} disabled={!projectsQuery.hasNextPage || projectsQuery.isFetching} > {projectsQuery.isFetchingNextPage ? 'Loading more...' : projectsQuery.hasNextPage ? 'Load More' : 'Nothing more to load'} Fetching... ) } ``` [//]: # 'Example' [//]: # 'Example1' ```jsx projectsQuery.hasNextPage && !projectsQuery.isFetching && projectsQuery.fetchNextPage() } /> ``` [//]: # 'Example1' [//]: # 'Example3' ```tsx useInfiniteQuery(() => ({ queryKey: ['projects'], queryFn: fetchProjects, initialPageParam: 0, getNextPageParam: (lastPage, pages) => lastPage.nextCursor, getPreviousPageParam: (firstPage, pages) => firstPage.prevCursor, })) ``` [//]: # 'Example3' [//]: # 'Example4' ```tsx useInfiniteQuery(() => ({ queryKey: ['projects'], queryFn: fetchProjects, select: (data) => ({ pages: [...data.pages].reverse(), pageParams: [...data.pageParams].reverse(), }), })) ``` [//]: # 'Example4' [//]: # 'Example8' ```tsx useInfiniteQuery(() => ({ queryKey: ['projects'], queryFn: fetchProjects, initialPageParam: 0, getNextPageParam: (lastPage, pages) => lastPage.nextCursor, getPreviousPageParam: (firstPage, pages) => firstPage.prevCursor, maxPages: 3, })) ``` [//]: # 'Example8' [//]: # 'Example9' ```tsx return useInfiniteQuery(() => ({ queryKey: ['projects'], queryFn: fetchProjects, initialPageParam: 0, getNextPageParam: (lastPage, allPages, lastPageParam) => { if (lastPage.length === 0) { return undefined } return lastPageParam + 1 }, getPreviousPageParam: (firstPage, allPages, firstPageParam) => { if (firstPageParam <= 1) { return undefined } return firstPageParam - 1 }, })) ``` [//]: # 'Example9'
Loading...
Error: {projectsQuery.error.message}
{project.name}