Registry / testing / use-axios-request

use-axios-request

JSON →
library0.4.0jsnpmunverified

React hooks for data fetching with axios. Version 0.4.0 is the latest stable release (last updated 2021). Provides auto-cancellation on unmount, caching (no-cache, cache-first, cache-and-network), polling, and callbacks (onSuccess, onError). Fully typed with TypeScript. Requires React 16.8+ and axios 0.21.1+. Differentiates from react-query and SWR by being a simple hook wrapping axios with minimal abstraction.

npm install use-axios-request
INSTALL
IMPORT
SIG · USE-AXIOS-REQUEST
U
use-axios-request
testingjavascriptv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

useAxiosRequest
import { useAxiosRequest } from 'use-axios-request'
import useAxiosRequest from 'use-axios-request'
Named export only. Default import will fail.
useAxiosRequest
const { useAxiosRequest } = require('use-axios-request')
const useAxiosRequest = require('use-axios-request')
CommonJS users must destructure the named export.
DataTypeResponse (TypeScript)
const { data } = useAxiosRequest<DataTypeResponse>(config, options)
Generic type for response data; omitting it defaults to unknown.

Simple GET request with loading and error states, caching omitted.

import React from 'react'; import { useAxiosRequest } from 'use-axios-request'; const Avatar = ({ username }) => { const { isFetching, error, data } = useAxiosRequest( `https://api.github.com/users/${username}` ); if (isFetching) return 'Loading...'; if (error) return error.message || 'Error'; return <img src={data.avatar_url} alt="avatar" />; }; export default Avatar;
Debug
Known issues
gotchaPassing a new object config on every render causes infinite loop. useAxiosRequest triggers request on every new config reference.
fix
Memoize the config object with useMemo or useState.
affects: >=0.0.0
gotchaPolling with pollInterval > 0 does not stop when component unmounts? Auto-cancellation handles unmount, but polling may still trigger if config changes rapidly.
fix
Ensure config is stable or set pollInterval to 0 when not needed.
affects: >=0.0.0
gotchaonSuccess and onError callbacks are not stable; object identity issues may cause re-renders.
fix
Wrap callbacks in useCallback or use refs.
affects: >=0.0.0
deprecatedNo breaking changes documented; package appears stable but unmaintained since 2021.
fix
Consider migrating to react-query or SWR for active support.
affects: >=0.4.0
Errors
Common errors & fixes
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Calling useAxiosRequest inside a class component or outside React function.
fix
Move the hook inside a functional component.
Uncaught TypeError: Cannot destructure property 'isFetching' of 'undefined' or 'null'.
Hook returned undefined because config was null/undefined but no default? Actually useAxiosRequest allows null config; this error occurs if hook is called conditionally.
fix
Always call the hook at the top level, or handle conditional rendering with a wrapper component.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
axiosrequiredCore HTTP client; peer dependency
reactrequiredPeer dependency for hooks
react-domrequiredPeer dependency for web rendering
Agent activity
12 hits · last 30 days
node
10
Resources
use-axios-request — npm install use-axios-request · libregistry