Logo
View source code on GitHub

The same as useEffect, but for async functions.

useAsyncEffect(effect: () => Promise<void>, deps?: unknown[]): void;

An async function, such as data fetching, subscriptions and timers.

Cleanup function is currently not supported.

Dependencies of effect. The function will be executed every time one of the dependencies changes.

Default: undefined

import { useState } from "react";
import { useAsyncEffect } from "@mrcaidev/hooks";

export function Component() {
  const [message, setMessage] = useState("");

  useAsyncEffect(async () => {
    const data = await fetch("https://example.com");
    const json = await data.json();
    setMessage(json.message);
  }, []);

  return <p>{message}</p>;
}