useAsync

React 钩子,用于解析 async 函数或返回 promise 的函数。

用法

  1. import {useAsync} from 'react-use';
  2. // Returns a Promise that resolves after one second.
  3. const fn = () => new Promise((resolve) => {
  4. setTimeout(() => {
  5. resolve('RESOLVED');
  6. }, 1000);
  7. });
  8. const Demo = () => {
  9. const state = useAsync(fn);
  10. return (
  11. <div>
  12. {state.loading?
  13. <div>Loading...</div>
  14. : state.error?
  15. <div>Error...</div>
  16. : <div>Value: {state.value}</div>
  17. }
  18. </div>
  19. );
  20. };

参考

  1. useAsync(fn, args?: any[]);