Content
useFetch
Custom hook to fetch data from an API endpoint.
useFetch
Usage
API
useFetch
Fetches data from the provided URL using the specified request options.
This function uses fetch
with an AbortController
to allow for request cancellation if needed.
Updates the state with the response data, error, or loading status based on the fetch result.
Parameters
Name | Type | Description |
---|---|---|
url | string | The URL of the API endpoint. |
reqOpt? | RequestInit | Optional configuration for the fetch request (e.g., method, headers). |
T | T | Type of the data returned by the API. |
E | E | Type of the error returned by the API (default is string ). |
Returns
Name | Type | Description |
---|---|---|
data | T | The data returned by the API, or null if no data has been received yet. |
error | E | The error returned by the API, or null if no error has occurred. |
isPending | boolean | A boolean indicating whether the fetch request is currently in progress. |
isSuccess | boolean | A boolean indicating if the fetch request was successful. |
isError | boolean | A boolean indicating if the fetch request resulted in an error. |
refetch | () => void | A function to manually trigger the fetch request again. |