1.1.2 • Published 1 year ago

react-loading-hook v1.1.2

Weekly downloads
73
License
MIT
Repository
github
Last release
1 year ago

React Loading Hook

npm

Async loading and error state management hook for TypeScript and JavaScript React applications

Install

npm install react-loading-hook

or

yarn add react-loading-hook

Please note that TypeScript type definition files are included.

Quickstart

import React from "react";
import { useLoadingCallback } from "react-loading-hook";

export default function App() {
  const [person, setPerson] = React.useState();
  const [fetchPerson, isLoading, error, reset] = useLoadingCallback(
    async (id) => {
      const response = await fetch(`https://swapi.dev/api/people/${id}/`);
      const person = await response.json();

      setPerson(person);
    }
  );

  React.useEffect(() => {
    const personId = 1;

    fetchPerson(personId);
  }, []);

  if (error) {
    return <span>{error.message}</span>;
  }

  if (isLoading || !person) {
    return <span>Is loading...</span>;
  }

  return <span>{person.name}</span>;
}
1.1.1

1 year ago

1.1.2

1 year ago

1.1.0

2 years ago

1.0.0

3 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.1

5 years ago