useFetch()
The
useFetch() hook provides a declarative way to fetch data from APIs. It encapsulates the boilerplate logic for handling async requests, including loading indicators, error catching, and request cancellation cleanup.Import
typescript
Usage
Click a button to start fetching
Component.tsx
API
Arguments
url– string | undefined The URL to fetch data from. If undefined, the request will pause (idle state).options– RequestInit (Optional) Standard Fetch API options (method, headers, body, etc.).
Returns
Returns a state object with the following properties:
data– T | undefined The data received from the API response (parsed as JSON).error– Error | undefined Error object if the request failed or the response status was not OK.isLoading– boolean True while the request is in progress.