GithubHelp home page GithubHelp logo

arthurdenner / use-fetch Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 1.41 MB

React hook to fetch data with useful features.

License: MIT License

JavaScript 21.47% TypeScript 78.53%
react hooks fetch

use-fetch's Introduction

@arthurdenner/use-fetch

npm All Contributors gzip size install size

React hook to fetch data with useful features.

โš  This library was a good experiment, but currently there are better solutions to this problem, such as react-query, so I'd recommend avoiding this library as it won't be maintained anymore. โš 

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

name type required description
cacheKey string no localStorage key to store the response
expiryTime number no amount of time to cache the response
initialData T yes initial data for the hook
isJsonP boolean no indicates if the URL returns JSONP data
options RequestInit no options accepted by the fetch API
url string yes URL to be fetched

Return

name type description
abort () => void callback function to cancel the request
start () => void callback function to fire the request
data T data returned from the request
error Error | undefined error occurred on the request
loading boolean indicates if the request is being made
canceled boolean indicates 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

use-fetch's People

Contributors

dependabot-preview[bot] avatar renovate[bot] avatar renovate-bot avatar arthurdenner avatar

Stargazers

Alysson Lopes avatar Pedro Pereira avatar Emerson Laurentino avatar Leonardo Luiz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.