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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-smart-fetch

v1.0.2

Published

A simple React hook for smart API fetching with automatic caching support

Readme

React Smart Fetch

A simple React hook for smart API fetching with automatic caching support.

Features

  • 🚀 Simple API: Easy to use React hook
  • 📦 Smart Caching: Automatic response caching with configurable expiration
  • 🔄 Multiple HTTP Methods: Support for GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
  • Loading States: Built-in loading, error, and data states
  • 🔁 Manual Refetch: Refetch function to reload data on demand
  • 📱 TypeScript Support: Full TypeScript support with type safety
  • 🎯 Lightweight: Minimal bundle size with zero dependencies

Installation

npm install react-smart-fetch

or

yarn add react-smart-fetch

Quick Start

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

function UserProfile({ userId }) {
  const { data, error, loading, refetch } = useSmartFetch(`/api/users/${userId}`, {
    method: 'GET',
    cacheTime: 300000, // Cache for 5 minutes
  });

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

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

API Reference

useSmartFetch(url, options?)

Parameters

  • url (string): The API endpoint URL
  • options (object, optional): Configuration options
    • method (string): HTTP method - GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD (default: 'GET')
    • body (any): Request body for POST, PUT, PATCH methods
    • headers (object): Custom headers
    • cacheTime (number): Cache expiration time in milliseconds (default: 5 minutes)

Returns

  • data: The response data (null initially)
  • error: Error object if request fails (null initially)
  • loading: Loading state boolean
  • refetch: Function to manually refetch the data

Examples

GET Request with Caching

const { data, loading, error } = useSmartFetch('/api/posts', {
  cacheTime: 600000, // 10 minutes cache
});

POST Request

const { data, loading, error, refetch } = useSmartFetch('/api/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: {
    title: 'New Post',
    content: 'Post content...',
  },
});

PUT Request

const { data, loading, error } = useSmartFetch(`/api/posts/${postId}`, {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
  },
  body: updatedPost,
});

DELETE Request

const { data, loading, error } = useSmartFetch(`/api/posts/${postId}`, {
  method: 'DELETE',
});

Custom Headers

const { data, loading, error } = useSmartFetch('/api/protected', {
  headers: {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json',
  },
});

Caching Behavior

  • Responses are cached by URL + method + body combination
  • Cache is stored in memory and cleared on page refresh
  • Cached responses are returned immediately while a background request validates the cache
  • Cache expiration is configurable per request
  • Default cache time is 5 minutes (300000ms)

TypeScript Support

The package includes full TypeScript support:

interface User {
  id: number;
  name: string;
  email: string;
}

const { data, loading, error } = useSmartFetch<User>('/api/user/1');
// data is typed as User | null

Author

Darshit Gajjar
GitHub: gajjardarshithasmukhbhai

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request to the GitHub repository.

Visit Our Website