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

NameTypeDescriptionDefault
urlstringEndpoint to fetch.-
optionsRequestInit & { immediate?: boolean; onSuccess?: (data: any) => void; onError?: (error: any) => void }Fetch options plus immediate, onSuccess and onError handlers.-

Returns

NameDescription
dataParsed JSON data from the response.
errorError object, if the request failed.
isLoadingTrue while the request is in-flight.
isSuccessTrue when the last request succeeded.
isErrorTrue 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.
Saha UI - The Modern React Component Library