interface Item { id: number; name: string; }
interface Data { data: Item[]; total: number; }
interface FormState { name: string; gender: string; color: string[]; }
const ref = useRef<HTMLDivElement>(null) // The target element for bottom detection, needed only for scroll loading
const {
list, // The list of return values composed of the data returned by fetcher, Data[]
fullList, // The item list formed by unfolding the list in the data returned by fetcher, Item[]
form, // Form state and operations
selection, // Multi-selection state and operations
paginationState, // Pagination state, information of the requested page
loading, // Loading state
isLoadDone // Whether the loading is completed
} = useInfiniteList<Data, Item, FormState>({
target: ref, // The target element for bottom detection, needed only for scroll loading
fetcher: fetchPagination, // Function to fetch data
mapFullList: (d) => d.data, // Map function for obtaining data list from every return
canLoadMore: (previousData, dataList, fullList) => {
if (!previousData) return true // If there is no result from the last request, it means it's the first load
// Continue loading if the current list length is less than the total, otherwise stop loading
return fullList.length < previousData.total
},
form: { // Configure useForm
initialValue: {
name: '',
gender: 'Boy',
color: ['Red'],
},
},
pagination: { pageSize: 10 }, // Configure usePagination
immediateQueryKeys: ['color', 'gender'], // Form fields for immediate querying
})
return (
<div>
<MyForm form={form} />
<div ref={ref} className="h-360px overflow-scroll">
<MyInfiniteScrollList list={fullList} />
</div>
</div>
)