Skip to content
Permalink
Branch: master
Find file Copy path
2 contributors

Users who have contributed to this file

@streamich @xiaoxiangmoe
41 lines (30 sloc) 616 Bytes

useAsync

React hook that resolves an async function or a function that returns a promise;

Usage

import {useAsync} from 'react-use';

// Returns a Promise that resolves after one second.
const fn = () => new Promise((resolve) => {
  setTimeout(() => {
    resolve('RESOLVED');
  }, 1000);
});

const Demo = () => {
  const state = useAsync(fn);

  return (
    <div>
      {state.loading?
        <div>Loading...</div>
        : state.error?
        <div>Error...</div>
        : <div>Value: {state.value}</div>
      }
    </div>
  );
};

Reference

useAsync(fn, args?: any[]);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.