Tired creating these state in your component: isLoading, isError, data, retry?

With use-promise-result, your job is to specify how to get data, we take care all the boring flags.


npm i use-promise-result


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

const dataProvider = async () => {
  return (await axios.get("")).data;

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

  // ...

Fetch data on action:

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

Passing the second parameter to fail would stop usePromiseResult from calling data provider on mount.

Later you can call reload function to trigger the call to data provider (ex: when user click on a button)

API Reference:


- usePromiseResult(<dataProvider>, [initFetch]) : {data, error, loading, success, reload}

Returned value

- data: value returned from dataProvider
- error: error throw from dataProvider
- loading: indicate state of the promise returned from dataProvider
- reload: call this function to reload data
- reloadCount: number of time dataProvider get involved, use this with initFetch = false

Most of the time, you only care about is data available or not

- success: success = !state.loading && !state.error