1.1.5 • Published 5 years ago

@jsxtools/use-fetch-factory v1.1.5

Weekly downloads
1
License
CC0-1.0
Repository
github
Last release
5 years ago

use-fetch-factory

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
});
1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago