useFetch Hook
A React hook for fetching data with loading, error, and result states. Easy abort and error handling.
Installation
Pre-requisite: You need to have react
and react-dom
installed in your project.
Simple Usage
API Reference
useFetch
A hook to make requests with the Fetch API in React.
Parameters
url: URL | string | Request
: The URL to request.options?: RequestInit
: The options to pass to fetch.config?: Partial<FetchConfig>
: The configuration for the request.
Returns
An object containing the following properties:
mutate: () => Promise<void>
: A function to make the request.abort: () => void
: A function to abort the current request.data: T | undefined
: The response data.error: HTTPError | undefined
: The error object if the request fails.loading: boolean
: A boolean indicating whether the request is in progress.
HTTPError
An error class representing an HTTP error.
FetchConfig
A type representing the configuration for the request.
FetchConfig
is a generic type that takes a type parameter T
representing the response data.
Example
Customizations
- You can provide additional options and configurations according to the Fetch API and the provided
FetchConfig
type.