A Light-weight And Simple Data Fetcher For React – use-promise-result

Category: React React Others | July 8, 2021
Author: lekhasy
Official Page: Go to website
Last Update: July 8, 2021
License: MIT

data fetcher for react

Description:

Are you tired of creating the states in your component such as isLoading, isError, data, retry? Then, use-promise-result library is for you, which is light-weight and simple data fetcher for React component. You will just need to specify how to get data and this component will do everything.

How to use it?

  1. You will need to install the data fetcher for React component with NPM, like this:
npm i use-promise-result

2. Then, import the usePromiseResult in your component.

import { usePromiseResult } from "use-promise-result";

3. And, the code that will do everything.

const dataProvider = async () => {
  return (await axios.get("https://jsonplaceholder.typicode.com/todos/1")).data;
};

function App() {
  const { data, error, loading, success, reload } =
    usePromiseResult(dataProvider);

  // ...
}

That’s it. 🙂