🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

@arthurdenner/use-fetch

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arthurdenner/use-fetch

React hook to fetch data with useful features.

latest
Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
40
135.29%
Maintainers
1
Weekly downloads
 
Created
Source

@arthurdenner/use-fetch

npm All Contributors gzip size install size

React hook to fetch data with useful features.

Install

yarn add @arthurdenner/use-fetch
npm i @arthurdenner/use-fetch

Usage

import React from 'react';
import useFetch from '@arthurdenner/use-fetch';

function App() {
  const { data: users, error, loading } = useFetch({
    initialData: [],
    url: 'https://jsonplaceholder.typicode.com/users',
  });

  if (error) {
    console.log(error);
    return <div>An error occured!</div>;
  }

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <ul>
        {users.map(user => (
          <li>{user.name}</li>
        ))}
      </ul>
    </div>
  );
}

Features

  • TypeScript-friendly
  • JSONP support
  • localStorage cache support
    • You don't pass a cacheKey, the hash of the URL will be used
  • Abort request support (manually and when the component unmounts)

Config

nametyperequireddescription
cacheKeystringnolocalStorage key to store the response
expiryTimenumbernoamount of time to cache the response
initialDataTyesinitial data for the hook
isJsonPbooleannoindicates if the URL returns JSONP data
optionsRequestInitnooptions accepted by the fetch API
urlstringyesURL to be fetched

Return

nametypedescription
abort() => voidcallback function to cancel the request
start() => voidcallback function to fire the request
dataTdata returned from the request
errorError | undefinederror occurred on the request
loadingbooleanindicates if the request is being made
canceledbooleanindicates if the request was canceled

Contributors

Thanks goes to these wonderful people (emoji key):

Arthur Denner
Arthur Denner

💻 🎨 📖 💡 🤔 🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © Arthur Denner

Keywords

react

FAQs

Package last updated on 03 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts