A type-safe React hook for data fetching with caching, error handling, and request cancellation.
- Overview
- Features
- Installation
- Quick Start
- API Reference
- Examples
- Project Structure
- Performance
- Contributing
- License
useFetch is a production-ready custom React hook that solves common data fetching challenges. Built with TypeScript, it provides automatic caching, loading states, error handling, and request cancellation out of the box.
- Zero external dependencies - Pure React implementation
- Full TypeScript support - Complete type safety with generics
- Intelligent caching - In-memory cache with configurable TTL
- Race condition safe - Automatic request cancellation
- Simple, predictable API - Easy to learn, powerful to use
| Feature | Description |
|---|---|
| 🔷 TypeScript | Full generic support for type safety |
| 💾 Caching | In-memory cache with configurable TTL |
| ⏳ Loading States | Built-in loading state management |
| Comprehensive error catching | |
| 🛑 Request Cancellation | Aborts in-flight requests on unmount |
| 🎯 Conditional Fetching | Fetch only when needed |
| 🔄 Manual Refetch | Programmatic refresh capability |
| 📞 Callbacks | onSuccess and onError hooks |
| ⏱️ Timeout Support | Configurable request timeout |
| 🔍 Stale Detection | Check if cached data is stale |
# npm
npm install @kalaiarasan/use-fetch-hook
# yarn
yarn add @kalaiarasan/use-fetch-hook
Requirements
Node.js >= 18.0.0
React >= 18.0.0
TypeScript >= 5.0.0