useFetch
Custom hook that provides a wrapper around the native [
fetch API](https://developer.Installation
npx sse-tool add use-fetch
npm install sse-hooks
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),
},
);
import { useFetch } from "sse-hooks";
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
| Name | Type | Description |
|---|---|---|
| url | string | The URL to fetch. |
| options | UseFetchOptions | Options for customizing the request and hook behavior (optional). |
Return Value
Returns UseFetchReturn.
An object containing the fetched data, loading status, error, and methods to control the request.