typescriptintermediate
useFetch — Generic Data Fetching Hook
Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, useEffect, useCallback } from 'react';
interface UseFetchResult<T> {
data: T | null;
error: string | null;
loading: boolean;
refetch: () => void;
}
export function useFetch<T>(url: string): UseFetchResult<T> {
const [data, setData] = useState<T | null>(null);
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const fetchData = useCallback(() => {
let cancelled = false;
setLoading(true);
setError(null);
fetch(url)
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
})
.then((json) => {
if (!cancelled) setData(json as T);
})
.catch((err) => {
if (!cancelled) setError(err.message);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => { cancelled = true; };
}, [url]);
useEffect(() => {
const cleanup = fetchData();
return cleanup;
}, [fetchData]);
return { data, error, loading, refetch: fetchData };
}
// Usage:
// const { data, loading, error } = useFetch<User[]>('/api/users');Use Cases
- API data fetching
- Dashboard widgets
- Client-side data loading
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
useAsync Hook for Promise Management
Manage async operations with loading, error, and data states using a reusable hook pattern.
Best for: Data fetching with status tracking
#react#hooks
typescriptadvanced
useFetch with Cache Hook
Data fetching hook with built-in cache, loading, error states, and automatic revalidation.
Best for: API data fetching
#hooks#fetch
typescriptintermediate
React Custom Hook for Data Fetching
A reusable useFetch hook with loading states, error handling, caching, and abort support.
Best for: Reusable data fetching across components
#react#hooks
typescriptbeginner
useAbortController Hook
Creates and manages AbortController instances for cancelling fetch requests on unmount.
Best for: Cancelling stale requests
#hooks#abort