useAxios()
ts
function useAxios<TData, TError>(options): UseAxiosReturn<TData, TError>;Defined in: hook.ts:36
React hook for handling Axios requests with support for request cancellation and state management.
Main benefit of this hook is that it abstracts away the logic of handling request states and cancellation, providing a simple interface for making requests and reacting to their state changes.
Type Parameters
| Type Parameter | Description |
|---|---|
TData | Type of the response data. |
TError | Type of the response error. |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | UseAxiosOptions | UseAxiosOptions for the hook, including Axios request config and additional options for controlling the behavior of the hook. |
Returns
UseAxiosReturn<TData, TError>
An UseAxiosReturn containing the request state, response data, and handler functions for executing and aborting the request.
Example
tsx
function MyComponent () {
const {data, isLoading, hasError, execute} = useAxios<{name: string, address: string}, {message: string}>({
method: "get",
url: "/api/user"
})
return (
<>
<p>{data.name}</p>
<p>{data.address}</p>
</>
)
}Since
1.0.0
Author
Simon Kovtyk