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

use-fetch-lib

v2.2.3

Published

A simple React Hook that make rest api call easier.

Downloads

47

Readme

use-fetch-lib 🔥

A simple React Hook that make rest api call easier
Api calls without ~~Promise~~ or ~~async/ await~~

Note: This is using the new React Hooks API

Install

$ npm i use-fetch-lib --save
$ yarn add use-fetch-lib

Alt text

✨ It features:

  • Simple and Lightweight
  • TypeScript ready
  • Support Data Mocking
  • SSR support
  • Request Cancellation on component unmount
  • Request Caching (In-Memory cache)
  • Local/Cache state mutation (update your data as you want)

How to use

https://github.com/kiranbhalerao123/use-fetch-lib-example

use-fetch-lib exposes two named exports to us,

  • UseFetchProvider
  • useFetch

1️⃣ UseFetchProvider

  • UseFetchProvider is just a React component that help the useFetch to configure it properly.
  • Just wrap your parent component with UseFetchProvider
  • pass it, baseUrl(string) and authorizationToken(string|Function)
<UseFetchProvider
  baseUrl="http://dummy.restapiexample.com"
  authorizationToken={useSelector((store: any) => store.token)}
>
  <App />
</UseFetchProvider>

2️⃣ useFetch

const [{ data, status }, recall, updateState] = useFetch({
  url: "/api/v1/employee/1",
  method: "get",
  shouldDispatch: true
});

alternate syntax ♦️
const [{ data, status }, recall] = useFetch("/api/v1/employee/1"); // note: Default value of `shouldDispatch` is false
  • This will get called on componentDidMount as we pass shouldDispatch true

  • it returns an array that we destruct as [{data, status}, recall]

  • data is an object return from Your api call

  • status active status of your api call, can be destruct as {isFulfilled, isPending, isRejected, isMocked, isCached, err}

  • recall it is a function to recall your api as you want

  • updateState is a function to update fetch state locally, it take callback function with preData as its arg.

    • ex. updateState((preData)=> updatedData)
  • Cache

    • You can cache your api req. by providing cache: true
    const [{ data: Posts, status }] = useFetch({
      url: "/posts",
      method: "get",
      cache: true,
    });
  • Typescript

    • we can pass generic types to useFetch
      const [{ data: Posts, status: { isFulfilled } }, postTodoService] = useFetch<IPostData, IPostTodo>({
        url: "/posts",
        method: "post"
      });
  • useFetch Params 👇

| name | Type | Default | Required | Description | | ------------------ | ------------------------ | ------- | -------- | ------------------------------------------------------------------------------------------------------------------ | | url | string | | required | The request URL | | method | string | get | optional | The request method 'get', 'delete', 'post', 'put' | | mockData | {} | | optional | This is default data for typescript types and api mocking | | shouldDispatch | () => boolean or boolean | false | optional | The conditions for auto run the service(on componentDidMount) | | cancelable | boolean | false | optional | Should cancel Api request on Component unmount | | shouldUseAuthToken | boolean | true | optional | if it is true it will send your authorizationToken with the request | | dependencies | Array | true | optional | This is dependencies array, if any of dependency get update them the service will re-call(on componentDidUpdate) | | before | () => void | | optional | This function will trigger before the api call triggers | | after | (state) => void | | optional | This function will trigger after the Api call | | alter | (state) => newState | | optional | With this function you can alter the api response as you want | | options | {} | | optional | The config options of Axios.js (https://goo.gl/UPLqaK) |