Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 859 Bytes

useAsyncRetry.md

File metadata and controls

46 lines (37 loc) · 859 Bytes

useAsyncRetry

使用useAsync和一个额外的retry方法来轻松重试/刷新异步函数;

Usage

import {useAsyncRetry} from 'react-use';

// Returns a Promise that resolves after one second.
const fn = () => new Promise((resolve, reject) => {
  setTimeout(() => {
    if (Math.random() > 0.5) {
      reject(new Error('Random error!'));
    } else {
      resolve('RESOLVED');
    }
  }, 1000);
});

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

  return (
    <div>
      {state.loading?
        <div>Loading...</div>
        : state.error?
        <div>Error...</div>
        : <div>Value: {state.value}</div>
      }
      {!state.loading?
        <a href='javascript:void 0' onClick={() => state.retry()}>Retry</a>
        : null
      }
    </div>
  );
};

Reference

useAsyncRetry(fn, args?: any[]);