Skip to content

React Fetch API Reference

The main hook for fetching data with caching and reactivity.

import { useFetch } from '@resourge/react-fetch';
const { data, error, loading, refetch } = useFetch('/api/data');
  • input: Request URL or Request object.
  • options (optional): Fetch options like method, headers, body, etc.
  • config (optional): Additional config for caching, revalidation, polling, etc.
PropertyDescription
dataThe response data
errorAny error caught during the fetch
loadingLoading state
refetchFunction to refetch the data

Hook for automatic pagination of data.

import { usePagination } from '@resourge/react-fetch';
const { data, loading, loadMore, hasMore } = usePagination('/api/items');

Hook for infinite loading of data.

import { useInfiniteLoading } from '@resourge/react-fetch';
const { data, loading, loadMore, hasMore } = useInfiniteLoading('/api/items');

Keeps or restores the scroll position when navigating.

import { useScrollRestoration } from '@resourge/react-fetch';
useScrollRestoration();

Combines infinite scroll with scroll position restoration.

import { useInfiniteScrollRestoration } from '@resourge/react-fetch';
useInfiniteScrollRestoration();

Refetches data when dependencies change.

import { useFetchOnDependencyUpdate } from '@resourge/react-fetch';
const data = useFetchOnDependencyUpdate('/api/data', [dependency]);

Detects if the user is online or offline.

import { useIsOnline } from '@resourge/react-fetch';
const isOnline = useIsOnline();

Visual component to indicate loading state.

import { Loader } from '@resourge/react-fetch';
<Loader />

Global loader for the entire application.

import { GlobalLoader } from '@resourge/react-fetch';
<GlobalLoader />

Fallback component displayed during loading.

import { LoadingFallback } from '@resourge/react-fetch';
<LoadingFallback />

Component to integrate with React Suspense.

import { LoadingSuspense } from '@resourge/react-fetch';
<LoadingSuspense />

Pull-to-refresh control for mobile devices.

import { RefreshControl } from '@resourge/react-fetch';
<RefreshControl onRefresh={() => { /* refresh logic */ }} />