generates a hook that provides the state and settled response of a fetch request
npm install @jsxtools/use-fetch-factoryuse-fetch-factory generates a [hook] that provides the state and settled response of a [fetch].
It is 597 bytes (351 gzipped).
``sh`
npm install @jsxtools/use-fetch-factory
`js
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"response
// the is the response from the fetch
const [ state, response ] = useFetch('https://httpbin.org/get');
return state === 'pending'
? 'Loading'
: JSON.stringify(settledValue);
}
`
`js
const [ state, response, abort ] = useFetch('https://httpbin.org/get');
// abort the fetch
abort();
`
`js``
// abort the fetch after 2000ms
const [ state, response ] = useFetch('https://httpbin.org/get', {
timeout: 2000
});
[fetch]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
[frontend]: https://github.com/jsxtools/frontend