Introduction
Custom hooks in React enable you to extract reusable logic — such as making API requests — into neat, reusable functions. This makes your components cleaner, easier to read, and simpler to test. In this guide, you'll learn how to create safe, efficient, and reusable custom hooks for fetching data from APIs in React.
Why Use Custom Hooks for API Calls?
Reusability — Put common API logic (fetching, loading state, error handling) in one place and reuse it across components.
Cleaner Components — Components become mostly about UI; data fetching moves into hooks.
Testability — Hooks are easy to test independently from UI.
Consistency — One hook enforces a consistent pattern for all API calls (caching, retries, aborting).
Using custom hooks helps teams, especially developers in India, the USA, and the UK, keep large React apps maintainable and fast.
Basic Pattern: useFetch Hook
A simple reusable hook for GET requests. It manages three states: data, loading, and error.
import { useState, useEffect } from 'react';
export function useFetch(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
if (!url) return;
let isMounted = true; // avoid state update after unmount
async function fetchData() {
setLoading(true);
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
const json = await res.json();
if (isMounted) setData(json);
} catch (err) {
if (isMounted) setError(err);
} finally {
if (isMounted) setLoading(false);
}
}
fetchData();
return () => {
isMounted = false;
};
}, [url]);
return { data, loading, error };
}
How to use it in a component:
import React from 'react';
import { useFetch } from './hooks/useFetch';
function Posts() {
const { data, loading, error } = useFetch('https://jsonplaceholder.typicode.com/posts');
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<ul>
{data.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}
export default Posts;
Advanced Hook: useApi with POST, cancelation, and headers
For real apps you often need more control: custom headers, POST requests, request cancellation (AbortController), and manual triggers.
import { useState, useRef, useEffect, useCallback } from 'react';
export function useApi(defaultUrl = '', defaultOptions = {}) {
const [url, setUrl] = useState(defaultUrl);
const [options, setOptions] = useState(defaultOptions);
const [data, setData] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const abortCtrlRef = useRef(null);
const callApi = useCallback(async (callUrl = url, callOptions = options) => {
if (!callUrl) return;
setLoading(true);
setError(null);
abortCtrlRef.current?.abort(); // abort previous
const controller = new AbortController();
abortCtrlRef.current = controller;
try {
const res = await fetch(callUrl, { signal: controller.signal, ...callOptions });
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
const json = await res.json();
setData(json);
return json;
} catch (err) {
if (err.name === 'AbortError') {
// request was cancelled
return;
}
setError(err);
throw err;
} finally {
setLoading(false);
}
}, [url, options]);
useEffect(() => {
// optional: auto-call when url changes
if (url) callApi();
return () => abortCtrlRef.current?.abort();
}, [url, callApi]);
return {
data,
loading,
error,
callApi,
setUrl,
setOptions,
};
}
Join the conversation! Your thoughts help the community grow.