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
Argument | Description | Type | DefaultValue |
---|---|---|---|
effect | effect that support promise | () => T | Promise<T> (Required) | - |
cleanup | cleanup function | (() => T | Promise<T>) | undefined | () => {} |
deps | dependency list | React.DependencyList | undefined | - |