Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 606 Bytes

useAsync.md

File metadata and controls

35 lines (28 loc) · 606 Bytes

useAsync

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

Usage

import {useAsync} from 'react-use';

const Demo = ({url}) => {
  const state = useAsync(async () => {
    const response = await fetch(url);
    const result = await response.text();
    return result
  }, [url]);

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

Reference

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