Wraps an async function with loading and error state, and handles cancellation.
The arguments of the async function.
The error type.
The async function to wrap.
A tuple [callback, state].
[callback, state]
The callback receives an AbortSignal as its last argument. Previous executions are aborted when a new one starts.
AbortSignal
const [fetchData, { loading, error }] = useAsyncCallback(async (id: string, signal) => { const response = await fetch(`/api/${id}`, { signal }); // ...}); Copy
const [fetchData, { loading, error }] = useAsyncCallback(async (id: string, signal) => { const response = await fetch(`/api/${id}`, { signal }); // ...});
Wraps an async function with loading and error state, and handles cancellation.