use-smart-fetch
v1.0.6
Published
useSmartFetch is a custom React hook that simplifies making HTTP requests using the native fetch API while automatically managing loading states, errors, caching, and expiration.
Maintainers
Readme
useSmartFetch
useSmartFetch is a custom React hook that simplifies making HTTP requests using the native fetch API while automatically managing loading states, errors, caching, and expiration.
📖 Table of Contents
- ✨ Features
- 📥 Installation
- 🚀 Usage
- 🛠 Caching Mechanism
- 📌 API
- ⚙️ Customization
- 🤝 Contributing
- 📜 License
- 👨💻 Author
✨ Features
✅ Simplified HTTP requests – Easily fetch data from any API using native fetch.
✅ State management – Automatically handles loading, error, and data states.
✅ Cache support – Stores responses to avoid redundant API requests.
✅ Cache expiration – Automatically invalidates old data after a set duration.
✅ Refetch functionality – Allows manual re-fetching of data when needed.
📥 Installation
You can install this library via npm or yarn:
npm install use-smart-fetchor
yarn add use-smart-fetchAlternatively, if you prefer manual installation, simply copy the useSmartFetch.tsx file into your project.
🚀 Usage
Import the hook into your React component and provide the API URL along with any options.
"use client";
import React from "react";
import { useSmartFetch } from "use-smart-fetch"; // Import the hook
export default function App() {
const { data, loading, error, refetch } = useSmartFetch<Array<any>>({
url: "https://jsonplaceholder.typicode.com/posts",
});
if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error}</p>;
return (
<div>
<h1>Posts List</h1>
<button onClick={refetch}>Refetch Data</button>
<ul>
{data?.map((post: any) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</div>
);
}🛠 Caching Mechanism
The hook uses a Map to store fetched data along with an expiration timestamp.
How it works:
- A unique cache key is generated based on the URL and request options.
- The hook checks if a valid cache entry exists:
- ✅ If valid, it returns the cached data immediately.
- ❌ If expired or not found, a new API request is made.
- The cache expiration time is customizable via the
cacheDurationparameter (default: 300,000ms or 5 minutes).
📌 API
useSmartFetch<T>(url: string, options?: RequestInit, cacheDuration?: number)
Parameters:
url(string) → The API URL to fetch data from.options(RequestInit, optional) → Additional fetch options (headers, method, body, etc.).cacheDuration(number, optional) → Time in milliseconds before cache expires (default = 300,000 ms = 5 minutes).
Returns:
An object containing:
data: The fetched data (nullif not available).loading: Abooleanindicating if the request is in progress.error: Astringdescribing any error encountered.refetch: A function to manually re-trigger the fetch request.
⚙️ Customization
You can modify the hook to fit your needs:
- Adjust
cacheDurationto store data for a longer or shorter period. - Extend cache invalidation logic (e.g., implement LRU cache strategies).
- Modify request handling to work with different response types or APIs.
🤝 Contributing
Feel free to open issues or submit pull requests if you have suggestions, improvements, or feature requests!
📜 License
This project is licensed under the MIT License.
👨💻 Author
Developed by @Joao Pacheco.
