const useInfiniteList = < T > (
fetchFunction: (offset: number, limit: number) => Promise<T[]>,
const [data, setData] = useState < T []>([]);
const [loading, setLoading] = useState ( false );
const [hasMore, setHasMore] = useState ( true );
const [error, setError] = useState < string | null >( null );
const loadMore = useCallback ( async () => {
if (loading || ! hasMore) return ;
const newData = await fetchFunction (data . length , limit);
setData ( prev => [ ... prev, ... newData]);
setHasMore (newData . length === limit);
setError (err instanceof Error ? err . message : ' Failed to load data ' );
}, [data . length , loading, hasMore, fetchFunction, limit]);
const refresh = useCallback ( async () => {
const newData = await fetchFunction ( 0 , limit);
setHasMore (newData . length === limit);
setError (err instanceof Error ? err . message : ' Failed to refresh data ' );
}, [fetchFunction, limit]);
const reset = useCallback ( () => {
const { data, loading, hasMore, loadMore, refresh } = useInfiniteList (
keyExtractor = { keyExtractor }
infiniteScroll = { { onLoadMore: loadMore , loading , hasMore } }
< RefreshControl refreshing = { false } onRefresh = { refresh } />