import React from 'react'
import Link from 'next/link'
import axios from 'axios'
import { useInView } from 'react-intersection-observer'
import { useInfiniteQuery, QueryClient, QueryClientProvider } from 'react-query'
import { ReactQueryDevtools } from 'react-query/devtools'
const queryClient = new QueryClient()
export default function App() {
return (
)
}
function Example() {
const { ref, inView } = useInView()
const {
status,
data,
error,
isFetching,
isFetchingNextPage,
isFetchingPreviousPage,
fetchNextPage,
fetchPreviousPage,
hasNextPage,
hasPreviousPage,
} = useInfiniteQuery(
'projects',
async ({ pageParam = 0 }) => {
const res = await axios.get('/api/projects?cursor=' + pageParam)
return res.data
},
{
getPreviousPageParam: firstPage => firstPage.previousId ?? undefined,
getNextPageParam: lastPage => lastPage.nextId ?? undefined,
}
)
React.useEffect(() => {
if (inView) {
fetchNextPage()
}
}, [inView])
return (
Infinite Loading
{status === 'loading' ? (
Loading...
) : status === 'error' ? (
Error: {error.message}
) : (
<>
{data.pages.map(page => (
{page.data.map(project => (
{project.name}
))}
))}
{isFetching && !isFetchingNextPage
? 'Background Updating...'
: null}
>
)}
Go to another page
)
}