npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-smart-fetch-hook

v1.0.1

Published

[![npm version](https://img.shields.io/npm/v/react-smart-fetch-hook.svg)](https://www.npmjs.com/package/react-smart-fetch-hook) [![npm downloads](https://img.shields.io/npm/dm/react-smart-fetch-hook.svg)](https://www.npmjs.com/package/react-smart-fetch-h

Downloads

8

Readme

react-smart-fetch-hook 🚀

npm version npm downloads bundle size license

A smart, lightweight React hook for data fetching with built-in caching, retries, timeouts, and refresh logic. Perfect for modern React applications that need resilient data fetching.

✨ Features

  • 🔄 Automatic Retries - Configurable retry logic for failed requests
  • 💾 Smart Caching - localStorage-based caching with expiry
  • ⏱️ Request Timeouts - Prevent hanging requests
  • 🔄 Auto Refresh - Polling/refetching at intervals
  • 🚫 Request Cancellation - AbortController support
  • 📦 Tiny Size - Less than 3kB gzipped
  • 🛠 TypeScript Ready - Full type definitions included
  • ⚛️ React 16.8+ Compatible - Works with all modern React versions

📦 Installation

npm install react-smart-fetch-hook
# or
yarn add react-smart-fetch-hook

🚀 Basic Usage

import { useSmartFetch } from 'react-smart-fetch-hook';

function UserProfile({ userId }) {
  const { data, error, loading, refetch } = useSmartFetch(
    `https://api.example.com/users/${userId}`,
    {
      cacheKey: `user-${userId}`,
      retry: 2,
      timeout: 5000
    }
  );

  if (loading) return <div>Loading user data...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return (
    <div>
      <h1>{data.name}</h1>
      <button onClick={refetch}>Reload Data</button>
    </div>
  );
}

🔧 API Reference

useSmartFetch(url: string, options?: Options)

Options

| Option | Type | Default | Description | |-------------------|------------|-----------|-------------| | cacheKey | string | undefined | Unique key for localStorage caching | | retry | number | 0 | Number of retry attempts on failure | | refetchOnMount | boolean | true | Fetch data when component mounts | | refreshInterval | number | 0 | Auto-refetch interval in milliseconds | | cacheExpiry | number | undefined | Cache expiration time in seconds | | timeout | number | 5000 | Request timeout in milliseconds | | deps | any[] | [] | Dependencies that trigger refetch when changed | | initialData | any | null | Initial data before first fetch |

Return Object

| Property | Type | Description | |-----------|--------------------|-------------| | data | T \| null | Fetched data (or cached data if available) | | error | Error \| null | Error object if request fails | | loading | boolean | true while request is in progress | | refetch | () => Promise<void> | Function to manually trigger refetch |

🎯 Advanced Examples

Auto-Refreshing Data (Polling)

const { data } = useSmartFetch('/api/stock-prices', {
  refreshInterval: 10000, // Refetch every 10 seconds
});

Cached Data with Expiration

const { data } = useSmartFetch('/api/weather', {
  cacheKey: 'weather-data',
  cacheExpiry: 3600, // 1 hour expiration
});

Dependent Fetching

const { data } = useSmartFetch(`/api/user/${userId}/posts`, {
  deps: [userId], // Refetches when userId changes
});

Error Recovery with Retries

const { data, error } = useSmartFetch('/api/unstable-endpoint', {
  retry: 3, // Will retry 3 times before giving up
  retryDelay: 1000, // Wait 1 second between retries
});

🤔 FAQ

How does caching work?

The hook stores responses in localStorage with the provided cacheKey. If cacheExpiry is set, it will ignore cached data after the expiration time.

Can I use this with GraphQL?

Yes! Just pass your GraphQL endpoint and handle the request body accordingly. For advanced GraphQL features, consider wrapping the hook.

How do I handle POST requests?

This hook is designed for GET requests by default. For other methods, consider extending it or using a more specialized solution.

🤝 Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you'd like to change.

📜 License

MIT © Abdullah Al Mubin