Skip to content
React hook that does a fetch and aborts when the components is unloaded or a different request is made
TypeScript JavaScript HTML
Branch: master
Clone or download
mauricedb Merge pull request #122 from mauricedb/greenkeeper/@typescript-eslint…
…/eslint-plugin-2.3.0

Update @typescript-eslint/eslint-plugin to the latest version 🚀
Latest commit 0fe22e8 Sep 16, 2019

README.md

use-abortable-fetch

Build Status Greenkeeper badge

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:

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 <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;
  if (!data) return null;

  return <div>Joke: {data.value.joke}</div>;
};

export default ChuckNorrisJoke;

See this CodeSandbox for a running example.

You can’t perform that action at this time.