Skip to content

Instantly share code, notes, and snippets.

View ahmad742's full-sized avatar
🏠
Freelancer

Ahmad Amir ahmad742

🏠
Freelancer
View GitHub Profile
@ahmad742
ahmad742 / useFetch.ts
Last active February 25, 2026 08:14
🔥 Advanced Production-Ready Data Fetching Hook with TypeScript & AbortController
import { useState, useEffect, useCallback } from 'react';
/**
* @description Advanced hook for API calls with cleanup & type safety.
* Demonstrates: Memory management, Type Generics, and Error Handling.
*/
export const useFetch = <T>(url: string) => {
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);