Installation
npx sse-hooks add use-fetch
yarn dlx sse-hooks add use-fetch
pnpm dlx sse-hooks add use-fetch
deno run -A npm:sse-hooks add use-fetch
bunx sse-hooks add use-fetch
Usage
import { useFetch } from "./{hooks file}";
interface User {
id: number;
name: string;
}
const { data, loading, error, execute } = useFetch<User>(
"https://api.example.com/user/1",
{
immediate: true,
onSuccess: (data) => console.log("User loaded:", data),
},
);
API
Parameters
| Prop | Default | Type |
|---|---|---|
options |
|
Options for customizing the request and hook behavior (optional).
|
url | - |
The URL to fetch. |
Returns
| Return Value | Default | Type |
|---|---|---|
abort | - |
Aborts the current request if it is pending. |
data | - |
The data received from the response, or null if not yet received. |
error | - |
The error object if the request failed, or null if successful. |
execute | - |
Manually triggers the fetch request. |
loading | - |
Indicates if the request is currently in progress. |
reset | - |
Resets the state to its initial values (data: null, loading: false, error: null). |
Types Aliases
UseFetchState
The state representation of the fetch request.
| Property | Type |
|---|---|
data |
The data received from the response, or null if not yet received. |
error |
The error object if the request failed, or null if successful. |
loading |
Indicates if the request is currently in progress. |
Changelog
useCookie
Custom hook that manages state synchronized with a browser `cookie`. It handles serialization, prefixes, updates across tabs, and custom event synchronization.
useNetworkInformation
Custom hook that tracks the device's network connection status and details (speed, type) using the Network Information API.