New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

use-fetch-pro

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-fetch-pro

A powerful React hook for data fetching with support for caching, retries, and cancellation.

latest
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

useFetchPro

A powerful and lightweight custom React hook for data fetching. It extends the native Fetch API with out-of-the-box support for request cancellation, caching, and retry logic.

Features

  • State Management: Handles loading, data, and error states automatically.
  • Retry Logic: Automatically retries failed requests.
  • Cancellation: Cancels pending requests on component unmount or when dependencies change.
  • Caching: In-memory caching to prevent redundant API calls.
  • Manual Refetch: A refetch function to trigger requests manually.

Installation

npm install use-fetch-pro

Usage

import React from 'react';
import { useFetchPro } from 'use-fetch-pro';

interface Post {
  id: number;
  title: string;
  body: string;
}

const MyComponent = () => {
  const { data, loading, error, refetch } = useFetchPro<Post[]>(
    'https://jsonplaceholder.typicode.com/posts',
    {
      cache: true,      // Enable caching
      retry: 3,         // Retry up to 3 times on failure
      retryDelay: 1000, // Wait 1 second between retries
    }
  );

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

  return (
    <div>
      <button onClick={refetch}>Refetch Posts</button>
      <ul>
        {data?.map(post => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
    </div>
  );
};

export default MyComponent;

API

useFetchPro<T>(url, options)

Parameters

  • url (string): The URL to fetch data from.
  • options (object, optional): Configuration for the fetch request.

Options

OptionTypeDefaultDescription
methodstring'GET'HTTP request method.
headersobjectundefinedRequest headers.
bodyBodyInitundefinedRequest body.
retrynumber0Number of times to retry on failure.
retryDelaynumber1000Delay in milliseconds between retries.
cachebooleanfalseIf true, caches the response in memory.
cacheTimenumber300000Cache duration in milliseconds (5 minutes).

Return Value

An object containing:

PropertyTypeDescription
dataT | nullThe fetched data, or null if not yet fetched.
errorError | nullAn error object if the request fails, else null.
loadingbooleantrue while the request is in progress.
refetch() => voidA function to manually trigger the fetch request.

License

ISC

Keywords

react

FAQs

Package last updated on 25 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts