Skip to main content

useAsyncEffect

React useEffect with async await support. Note it don't support generator function

Usage

Live Editor

function Demo() {
  const [data, setData] = useState(0);

  useAsyncEffect(
    async () => {
      const result = await new Promise<number>((resolve) => {
        setTimeout(() => {
          resolve(200);
        }, 5000);
      });
      setData(result);
    },
    () => {},
    [],
  );
  return <div>data: {data}</div>;
};

Result
Loading...

API

useAsyncEffect

Returns

void

Arguments

ArgumentDescriptionTypeDefaultValue
effecteffect that support promise() => T | Promise<T> (Required)-
cleanupcleanup function(() => T | Promise<T>) | undefined() => {}
depsdependency listReact.DependencyList | undefined-