Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mauricedb/use-abortable-fetch
React hook that does a fetch and aborts when the components is unloaded or a different request is made
https://github.com/mauricedb/use-abortable-fetch
Last synced: 1 day ago
JSON representation
React hook that does a fetch and aborts when the components is unloaded or a different request is made
- Host: GitHub
- URL: https://github.com/mauricedb/use-abortable-fetch
- Owner: mauricedb
- License: mit
- Created: 2018-11-06T11:45:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-24T05:01:02.000Z (4 months ago)
- Last Synced: 2024-10-29T18:08:31.175Z (4 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/use-abortable-fetch
- Size: 5.56 MB
- Stars: 159
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-react-hooks - `use-abortable-fetch`
- awesome-react-hooks-cn - `use-abortable-fetch`
- awesome-list - use-abortable-fetch
- awesome-react-hooks - `use-abortable-fetch`
- awesome-react-hooks - `use-abortable-fetch`
- awesome-react-hooks - use-abortable-fetch - React hook that does a fetch and aborts when the components is unloaded or a different request is made (Packages)
README
# use-abortable-fetch
[![Build Status](https://travis-ci.org/mauricedb/use-abortable-fetch.svg?branch=master)](https://travis-ci.org/mauricedb/use-abortable-fetch)
React hook that does a fetch and aborts when the components is unloaded or a new fetch request is started.
# Installation
`npm install use-abortable-fetch`
or
`yarn add use-abortable-fetch`## Example usage:
```JavaScript
import React from 'react';
import useAbortableFetch from 'use-abortable-fetch';const ChuckNorrisJoke = () => {
const { data, loading, error, abort } = useAbortableFetch(
'//api.icndb.com/jokes/random/?limitTo=[nerdy]&escape=javascript'
);if (loading) return
Loading...;
if (error) returnError: {error.message};
if (!data) return null;return
Joke: {data.value.joke};
};export default ChuckNorrisJoke;
```See this [CodeSandbox](https://codesandbox.io/s/n5q6xmwwq0) for a running example.