The callback function that performs the side effect. It receives a function that returns a boolean indicating whether the component is currently mounted. The callback should return a promise that resolves to a cleanup function or void.
Optional deps: DependencyListAn array of dependencies. The side effect will run whenever one of the dependencies changes. If no array is provided, the side effect will run only once (similar to componentDidMount and componentWillUnmount lifecycle methods in class components).
useAsyncEffect(async (isMounted) => {
const response = await fetch('/api/data');
const data = await response.json();
if (!isMounted()) return;
// ... use data
return () => {
// ... cleanup
};
}, []);
A custom React hook that performs an asynchronous side effect.