@jsxtools/use-fetch-factory
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

use-fetch-factory

npm version build status issue tracker pull requests

use-fetch-factory generates a [hook] that provides the state and settled response of a fetch.

It is 597 bytes (351 gzipped).

Installation

npm install @jsxtools/use-fetch-factory

Usage

import { useEffect, useRef, useState } from 'react';
import useFetchFactory from '@jsxtools/use-fetch-factory';

const useFetch = useFetchFactory({ useEffect, useRef, useState });

function Component () {
  // the `state` is "pending", "fulfilled", or "rejected"
  // the `response` is the response from the fetch
  const [ state, response ] = useFetch('https://httpbin.org/get');

  return state === 'pending'
    ? 'Loading'
  : JSON.stringify(settledValue);
}

Abort

const [ state, response, abort ] = useFetch('https://httpbin.org/get');

// abort the fetch
abort();

Timeout

// abort the fetch after 2000ms
const [ state, response ] = useFetch('https://httpbin.org/get', {
  timeout: 2000
});

Readme

Keywords

none

Package Sidebar

Install

npm i @jsxtools/use-fetch-factory

Weekly Downloads

8

Version

1.1.5

License

CC0-1.0

Unpacked Size

13.2 kB

Total Files

7

Last publish

Collaborators

  • jonathantneal