Utility
useFetch
Simplified data fetching with loading states, error handling, and caching.
import { useFetch } from "saha-ui"Uses
Fetch JSON from a URL with loading and error states, abort handling, and optional immediate invocation.
Props
| Name | Type | Description | Default |
|---|---|---|---|
| url | string | Endpoint to fetch. | - |
| options | RequestInit & { immediate?: boolean; onSuccess?: (data: any) => void; onError?: (error: any) => void } | Fetch options plus immediate, onSuccess and onError handlers. | - |
Returns
| Name | Description |
|---|---|
| data | Parsed JSON data from the response. |
| error | Error object, if the request failed. |
| isLoading | True while the request is in-flight. |
| isSuccess | True when the last request succeeded. |
| isError | True when the last request failed. |
| refetch() | Trigger the fetch again. |
Example
const { data, isLoading, refetch } = useFetch('/api/items', { immediate: true });Notes
Uses AbortController to cancel in-flight requests on unmount or refetch; expects JSON responses and throws on non-2xx status.