State Management

useAsync

Handle async operations with loading, error, and success states automatically.

import { useAsync } from "saha-ui"

Uses

Wrap an async function to handle loading/success/error state and to provide an `execute` function; optional immediate execution.

Props

NameTypeDescriptionDefault
asyncFunctionfunctionFunction returning a Promise.-
options.immediatebooleanRun immediately on mount.false

Returns

NameDescription
data, error, isLoading, isSuccess, isErrorAsync state flags and data.
execute(...args)Run the async function and update state.
reset()Reset the internal state.

Example

const { data, isLoading, execute } = useAsync(fetchUsers, { immediate: false }); await execute();

Notes

Errors are re-thrown by `execute` to allow caller-level handling; state is maintained in the hook.
Saha UI - The Modern React Component Library