axios-hooks, react-query, swr, and use-http are all React libraries designed to simplify data fetching, caching, and state management for asynchronous operations. react-query (now @tanstack/react-query) and swr are the industry leaders, offering robust caching, background refetching, and mutation tools. axios-hooks is a specialized wrapper that ties fetching logic directly to the Axios HTTP client. use-http provides a lightweight, Promise-based hook for basic GET, POST, and other HTTP methods with built-in loading and error states. While all solve the problem of server state management, they differ significantly in architecture, maintenance status, and feature depth.
Managing server state in React applications requires more than just useEffect and fetch. The libraries axios-hooks, react-query, swr, and use-http all aim to solve this, but they take different approaches to caching, configuration, and maintenance. Let's look at how they handle real-world engineering challenges.
react-query has evolved into @tanstack/react-query. The original react-query package on npm is considered legacy.
// react-query (Legacy package name, modern usage requires TanStack)
import { useQuery } from '@tanstack/react-query';
function Profile() {
const { data } = useQuery(['user'], fetchUser);
return <div>{data.name}</div>;
}
swr is maintained by Vercel.
// swr
import useSWR from 'swr';
function Profile() {
const { data } = useSWR('/api/user', fetcher);
return <div>{data.name}</div>;
}
axios-hooks is a wrapper specifically for Axios.
// axios-hooks
import { useAxios } from 'axios-hooks';
function Profile() {
const [{ data, loading, error }] = useAxios('/api/user');
return <div>{data?.name}</div>;
}
use-http is a lightweight community package.
// use-http
import { useGet } from 'use-http';
function Profile() {
const { data, loading, error } = useGet('/api/user');
return <div>{data?.name}</div>;
}
Caching is where these libraries differ the most. react-query and swr have sophisticated cache layers. axios-hooks and use-http are more basic.
react-query caches by query key.
// react-query
useQuery({
queryKey: ['todos'],
queryFn: fetchTodos,
staleTime: 1000 * 60, // 1 minute
});
swr caches by URL or key.
dedupingInterval to prevent duplicate requests.// swr
useSWR('/api/todos', fetcher, {
dedupingInterval: 2000,
revalidateOnFocus: true,
});
axios-hooks caches based on request configuration.
useAxios options.// axios-hooks
useAxios('/api/todos', {
cache: {
interpretHeader: true,
methods: ['get'],
},
});
use-http has minimal built-in caching.
// use-http
// No direct cache config in hook options
const { response, get } = useGet('/api/todos');
// Caching logic usually handled outside or via custom hooks
Updating data (POST, PUT, DELETE) requires handling loading states and cache invalidation.
react-query uses useMutation.
// react-query
const mutation = useMutation({
mutationFn: postTodo,
onSuccess: () => {
queryClient.invalidateQueries(['todos']);
},
});
swr uses mutate.
// swr
const { mutate } = useSWR('/api/todos', fetcher);
async function addTodo() {
await mutate(createTodo(), { revalidate: true });
}
axios-hooks uses useManualAxios or standard useAxios.
// axios-hooks
const [{ data }, execute] = useManualAxios({
url: '/api/todos',
method: 'post',
});
// Manually clear cache if needed
use-http provides method-specific hooks like usePost.
// use-http
const { post, response } = usePost('/api/todos');
async function addTodo() {
await post({ title: 'New Todo' });
}
Setup complexity varies from zero-config to provider-heavy.
react-query requires a QueryClientProvider.
// react-query
const queryClient = new QueryClient();
function App() {
return (
<QueryClientProvider client={queryClient}>
<HomePage />
</QueryClientProvider>
);
}
swr requires an SWRConfig (optional but recommended).
// swr
function App() {
return (
<SWRConfig value={{ fetcher, errorRetryCount: 3 }}>
<HomePage />
</SWRConfig>
);
}
axios-hooks requires an AxiosProvider.
// axios-hooks
import { AxiosProvider } from 'axios-hooks';
import axios from 'axios';
function App() {
return (
<AxiosProvider axios={axios}>
<HomePage />
</AxiosProvider>
);
}
use-http requires a Provider for global options.
// use-http
import { Provider } from 'use-http';
function App() {
return (
<Provider url='https://api.example.com'>
<HomePage />
</Provider>
);
}
How each library exposes errors to the component.
react-query exposes error object.
isError boolean flag for conditional rendering.// react-query
const { error, isError } = useQuery(['user'], fetchUser);
if (isError) return <div>Error: {error.message}</div>;
swr exposes error object.
isValidating helps distinguish between loading and revalidating.// swr
const { error } = useSWR('/api/user', fetcher);
if (error) return <div>Failed to load</div>;
axios-hooks exposes error in the response tuple.
// axios-hooks
const [{ error }] = useAxios('/api/user');
if (error) return <div>Status: {error.response?.status}</div>;
use-http exposes error in the return object.
// use-http
const { error } = useGet('/api/user');
if (error) return <div>Something went wrong</div>;
| Feature | react-query | swr | axios-hooks | use-http |
|---|---|---|---|---|
| Maintenance | ā Active (TanStack) | ā Active (Vercel) | ā ļø Low Activity | ā ļø Low Activity |
| HTTP Client | Agnostic | Agnostic | Axios Only | Fetch/Axios |
| Caching | Advanced | Advanced | Basic | Minimal |
| Setup | Provider Required | Provider Optional | Provider Required | Provider Optional |
| Mutations | useMutation | mutate | useManualAxios | usePost/usePut |
react-query (via TanStack) is the heavy-duty choice šļø. It is best for complex dashboards, enterprise apps, and teams that need strict control over caching and background sync. Note that you should install @tanstack/react-query instead of the legacy react-query package.
swr is the lightweight champion š. It is perfect for content sites, Next.js projects, and developers who want powerful caching without the boilerplate. It feels very "React-native" in its simplicity.
axios-hooks is the Axios specialist š. Use it only if you are already committed to Axios and want a quick hook wrapper without adopting a full query client. Be aware of its slower update cycle.
use-http is the simple tool šØ. It works for small scripts, prototypes, or internal tools where setting up a query client is overkill. Do not use it for complex data synchronization needs.
Final Thought: For most professional production applications in 2024, the choice is between @tanstack/react-query and swr. The other two packages are viable for niche cases but lack the ecosystem momentum and feature depth required for large-scale architecture.
Choose axios-hooks if your project is strictly tied to the Axios ecosystem and you need a simple wrapper without the complexity of a full caching server. It is best for smaller applications where you want Axios interceptors to work seamlessly with React hooks without configuring a separate query client.
Choose react-query (specifically the modern @tanstack/react-query version) if you need a powerful, feature-complete solution for complex server state. It is the industry standard for enterprise applications requiring advanced caching, mutations, and devtools, though you must migrate to the TanStack namespace for ongoing support.
Choose swr if you prefer a lightweight, opinionated library built by Vercel that focuses on speed and simplicity. It is ideal for projects that need automatic revalidation on focus and a minimal API footprint, especially when using Next.js or Vercel infrastructure.
Choose use-http if you need a very simple, drop-in hook for basic CRUD operations without the overhead of a query client or cache configuration. It works well for small prototypes or internal tools where advanced caching strategies are unnecessary.
The license of this software has changed to AWISC - Anti War ISC license
React hooks for axios, with built-in support for server side rendering.
npm install axios axios-hooks
axiosis a peer dependency and needs to be installed explicitly
axios-hooks@5.x is compatible with axios@1.xaxios-hooks@4.x and below are compatible with axios@0.ximport useAxios from 'axios-hooks'
function App() {
const [{ data, loading, error }, refetch] = useAxios(
'https://reqres.in/api/users?delay=1'
)
if (loading) return <p>Loading...</p>
if (error) return <p>Error!</p>
return (
<div>
<button onClick={refetch}>refetch</button>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
)
}
The package exports one default export and named exports:
import useAxios, {
configure,
loadCache,
serializeCache,
makeUseAxios
} from 'axios-hooks'
The main React hook to execute HTTP requests.
url|config - The request URL or config object, the same argument accepted by axios.options - An options object.
manual ( false ) - If true, the request is not executed immediately. Useful for non-GET requests that should not be executed when the component renders. Use the execute function returned when invoking the hook to execute the request manually.useCache ( true ) - Allows caching to be enabled/disabled for the hook. It doesn't affect the execute function returned by the hook.ssr ( true ) - Enables or disables SSR supportautoCancel ( true ) - Enables or disables automatic cancellation of pending requests whether it be
from the automatic hook request or from the manual execute method[!IMPORTANT]
Default caching behavior can interfere with test isolation. Read the testing section for more information.
Returns
[{ data, loading, error, response }, execute, manualCancel]
data - The success response data property (for convenient access).
loading - True if the request is in progress, otherwise False.
error - The error value
response - The whole success response object.
execute([config[, options]]) - A function to execute the request manually, bypassing the cache by default.
config - Same config object as axios, which is shallow-merged with the config object provided when invoking the hook. Useful to provide arguments to non-GET requests.options - An options object.
useCache ( false ) - Allows caching to be enabled/disabled for this "execute" function.Returns
A promise containing the response. If the request is unsuccessful, the promise rejects and the rejection must be handled manually.
manualCancel() - A function to cancel outstanding requests manually.
Allows to provide custom instances of cache and axios and to override the default options.
cache An instance of lru-cache, or false to disable the cacheaxios An instance of axiosdefaultOptions An object overriding the default Hook options. It will be merged with the default options.Dumps the request-response cache, to use in server side rendering scenarios.
Returns
Promise<Array> A serializable representation of the request-response cache ready to be used by loadCache
Populates the cache with serialized data generated by serializeCache.
cache The serializable representation of the request-response cache generated by serializeCacheCreates an instance of the useAxios hook configured with the supplied cache, axios instance and default options.
cache An instance of lru-cache, or false to disable the cacheaxios An instance of axiosdefaultOptions An object overriding the default Hook options. It will be merged with the default options.Returns
An instance of useAxios React Hook which will always use the provided cache and axios instance.
The returned value, besides being a function that can be used as a React Hook, also contains the properties:
resetConfigureconfigureloadCacheserializeCachewhich are the same as the package's named exports but limited to the useAxios instance returned by makeUseAxios.
The arguments provided to useAxios(config[,options]) are watched for changes and compared using deep object comparison.
When they change, if the configuration allows a request to be fired (e.g. manual:false), any pending request is canceled and a new request is triggered, to avoid automatic cancellation you should use autoCancel:false option
Because of this, it's important to make sure that the arguments to useAxios preserve deep equality across component renders. This is often the case unless functions (e.g. axios transformers) are provided to a configuration object. In that case, those functions need to be memoized or they will trigger a request execution at each render, leading to an infinite loop.
Unless provided via the configure function, axios-hooks uses as defaults:
axios - the default axios package exportcache - a new instance of the default lru-cache package export, with no argumentsdefaultOptions - { manual: false, useCache: true, ssr: true, autoCancel: true }These defaults may not suit your needs, for example:
In such cases you can use the configure function to provide your custom implementation of both.
When
configureis used, it should be invoked once before any usages of theuseAxioshook
import { configure } from 'axios-hooks'
import LRU from 'lru-cache'
import Axios from 'axios'
const axios = Axios.create({
baseURL: 'https://reqres.in/api'
})
const cache = new LRU({ max: 10 })
configure({ axios, cache })
On the client, requests are executed when the component renders using a React useEffect hook.
This may be undesirable, as in the case of non-GET requests. By using the manual option you can skip the automatic execution of requests and use the return value of the hook to execute them manually, optionally providing configuration overrides to axios.
In the example below we use the useAxios hook twice. Once to load the data when the component renders, and once to submit data updates via a PUT request configured via the manual option.
import useAxios from 'axios-hooks'
function App() {
const [{ data: getData, loading: getLoading, error: getError }] = useAxios(
'https://reqres.in/api/users/1'
)
const [{ data: putData, loading: putLoading, error: putError }, executePut] =
useAxios(
{
url: 'https://reqres.in/api/users/1',
method: 'PUT'
},
{ manual: true }
)
function updateData() {
executePut({
data: {
...getData,
updatedAt: new Date().toISOString()
}
})
}
if (getLoading || putLoading) return <p>Loading...</p>
if (getError || putError) return <p>Error!</p>
return (
<div>
<button onClick={updateData}>update data</button>
<pre>{JSON.stringify(putData || getData, null, 2)}</pre>
</div>
)
}
The cancellation method can be used to cancel an outstanding request whether it be
from the automatic hook request or from the manual execute method.
In the example below we use the useAxios hook with its automatic and manual requests.
We can call the cancellation programmatically or via controls.
function App() {
const [pagination, setPagination] = useState({})
const [{ data, loading }, refetch, cancelRequest] = useAxios({
url: '/users?delay=5',
params: { ...pagination }
})
const handleFetch = () => {
setPagination({ per_page: 2, page: 2 })
}
const externalRefetch = async () => {
try {
await refetch()
} catch (e) {
// Handle cancellation
}
}
return (
<div>
<button onClick={handleFetch}>refetch</button>
<button onClick={externalRefetch}>External Refetch</button>
<button disabled={!loading} onClick={cancelRequest}>
Cancel Request
</button>
{loading && <p>...loading</p>}
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
)
}
axios-hooks seamlessly supports server side rendering scenarios, by preloading data on the server and providing the data to the client, so that the client doesn't need to reload it.
useAxios HTTP requests are executed on the serverserializeCache() in order to obtain a serializable representation of the request-response cachewindow global variableloadCache<!-- fragment of the HTML template defining the window global variable -->
<script>
window.__AXIOS_HOOKS_CACHE__ = {{{cache}}}
</script>
// server code for the server side rendering handler
import { serializeCache } from 'axios-hooks'
router.use(async (req, res) => {
const index = fs.readFileSync(`${publicFolder}/index.html`, 'utf8')
const html = ReactDOM.renderToString(<App />)
// wait for axios-hooks HTTP requests to complete
const cache = await serializeCache()
res.send(
index
.replace('{{{html}}}', html)
.replace('{{{cache}}}', JSON.stringify(cache).replace(/</g, '\\u003c'))
)
})
// client side code for the application entry-point
import { loadCache } from 'axios-hooks'
loadCache(window.__AXIOS_HOOKS_CACHE__)
delete window.__AXIOS_HOOKS_CACHE__
ReactDOM.hydrate(<App />, document.getElementById('root'))
Sometimes it is necessary to communicate with different APIs or use different caching strategies for different HTTP interactions.
makeUseAxios allows to create multiple instances of the useAxios React Hook which can be configured and managed independently.
In other words, makeUseAxios is a factory of useAxios, which returns a React Hook configured against the provided axios or cache instances.
This feature can also be used to create a single pre configured React Hook instance as an alternative to the global
configurefeature
import axios from 'axios'
import { makeUseAxios } from 'axios-hooks'
const useAxios = makeUseAxios({
axios: axios.create({ baseURL: 'https://reqres.in/api' })
})
function App() {
const [{ data, loading, error }, refetch] = useAxios('/users?delay=1')
if (loading) return <p>Loading...</p>
if (error) return <p>Error!</p>
return (
<div>
<button onClick={refetch}>refetch</button>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
)
}
Testing components that make use of the useAxios hook are susceptible to test isolation leakage because of default caching behavior. The following snippets can be used to disable caching while testing:
beforeAll(() => {
useAxios.configure({ cache: false })
})
axios-hooks depends on a native ES6 Promise implementation to be supported. If your environment doesn't support ES6 Promises, you can polyfill.
axios-hooks is heavily inspired by graphql-hooks,
developed by the awesome people at NearForm.
MIT