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: about 2 months ago
JSON representation

React hook that does a fetch and aborts when the components is unloaded or a different request is made

Lists

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) return
Error: {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.