Skip to main content
Version: v10.0.0

HyperFetch vs RTK Query

RTK Query is the data fetching layer that ships with Redux Toolkit. If your app already runs on Redux, it is the obvious first choice, and this page says so up front. If you are looking for an rtk query alternative because you don't use Redux, or don't want to add it, read on.

The two libraries are closer to each other than either is to TanStack Query or SWR. Both make you define your API up front, both send the requests themselves, and both generate typed hooks from those definitions. The difference is where the state lives and what comes built in.

What you'll learn
  1. How RTK queries and mutations are defined with createApi, and the HyperFetch equivalent.
  2. How tags compare to HyperFetch cache invalidation.
  3. How streaming updates, codegen and devtools differ.
  4. Where React Query vs RTK Query fits in, and when to pick each.

What RTK Query does well​

From the RTK Query overview and its comparison page:

  • No extra dependency for Redux users. It is "an optional addon included in the Redux Toolkit package", built on createSlice and createAsyncThunk.
  • One place for the whole API. You define "an API slice with multiple endpoints ahead of time", which lets mutations invalidate and refetch queries automatically.
  • Full visibility in Redux DevTools. "Because RTK Query dispatches normal Redux actions as requests are processed, all actions are visible in the Redux DevTools."
  • Works outside React. Redux Toolkit is UI-agnostic, so RTK Query can be used with any UI layer.

The RTK Query docs list the main reasons to use it: you already have a Redux app, you want Redux DevTools history, you want to integrate with the Redux ecosystem, or your logic must work outside React. If one of those describes you, stay.


Defining RTK queries and mutations​

An RTK Query API slice, adapted from the automated re-fetching guide:

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/" }),
tagTypes: ["Post"],
endpoints: (build) => ({
getPosts: build.query<Post[], void>({
query: () => "/posts",
providesTags: ["Post"],
}),
editPost: build.mutation<Post, Partial<Post> & Pick<Post, "id">>({
query: (body) => ({ url: `post/${body.id}`, method: "POST", body }),
invalidatesTags: ["Post"],
}),
}),
});

export const { useGetPostsQuery } = api;

fetchBaseQuery is described in the docs as "a small wrapper around fetch". The slice is then registered with your Redux store, or wrapped in <ApiProvider /> if you don't have a store.

The same API in HyperFetch is a client and a set of requests. No store, no provider required:

import { createClient } from "@hyper-fetch/core";

export const client = createClient({ url: "https://api.example.com" });

export const getPosts = client.createRequest<{ response: Post[] }>()({
endpoint: "/posts",
});

export const editPost = client
.createRequest<{ response: Post; payload: Partial<Post> }>()({
endpoint: "/posts/:postId",
method: "PATCH",
})
// Refresh the list after every successful edit
.setOptimistic(() => ({ invalidate: [getPosts] }));

Each request is its own export, so there is no single slice file that grows with every endpoint. URL params come from the endpoint string: editPost.setParams({ postId: 1 }) is required and typed before send() compiles.


Tags vs cache invalidation​

RTK Query's docs define tags as "just a name that you can give to a specific collection of data to control caching and invalidation behavior". Queries providesTags, mutations invalidatesTags, and matching subscribed queries refetch. Tags can carry ids, so editing post 5 only refetches data tagged { type: "Post", id: 5 }.

HyperFetch has no tags. You invalidate by request, by cache key string, or by RegExp:

// One request
client.cache.invalidate(getPosts);

// Every cached page of /posts, whatever the query params
client.cache.invalidate(new RegExp("GET_/posts"));

// After a mutation, declared on the request itself
editPost.setOptimistic(() => ({ invalidate: [getPosts] }));

Any mounted useFetch for an invalidated key fetches again. Tags are more declarative once you have many endpoints sharing data. HyperFetch's approach is more direct and needs no naming scheme. See cache revalidation.


Hooks​

RTK Query generates a hook per endpoint (useGetPostsQuery), returning data, error and isLoading among others.

HyperFetch uses two generic hooks that take any request:

import { useFetch, useSubmit } from "@hyper-fetch/react";

function Posts() {
const { data, loading, error } = useFetch(getPosts);
const { submit, submitting } = useSubmit(editPost.setParams({ postId: 1 }));

return <button onClick={() => submit({ payload: { title: "Updated" } })}>Rename</button>;
}

submit resolves with { data, error, status } and never throws. See useFetch and useSubmit.


Streaming and realtime​

RTK Query handles streaming updates with the onCacheEntryAdded lifecycle. You open the connection yourself (the examples use the browser WebSocket), wait for cacheDataLoaded, apply messages with updateCachedData, and close the socket when cacheEntryRemoved resolves. The docs suggest polling or tag invalidation for most updates and streaming for small, frequent changes.

HyperFetch ships the socket layer as a separate package. The sockets package has WebSocket and SSE adapters, reconnection, and typed listeners and emitters:

import { createSocketClient } from "@hyper-fetch/sockets";

export const socket = createSocketClient({ url: "wss://api.example.com" });
export const onPostEdited = socket.createListener<Post>()({ topic: "post-edited" });

onPostEdited.listen(() => client.cache.invalidate(getPosts));

Code generation​

RTK Query has a code generation tool for OpenAPI and GraphQL schemas. Its comparison page calls it "currently experimental".

HyperFetch's CLI generates a typed SDK from an OpenAPI or Swagger spec. The output is a createSdk schema you plug your own client into.


Devtools​

RTK Query requests are Redux actions, so Redux DevTools shows every request and state change. If you already debug with Redux DevTools, that is hard to beat.

HyperFetch uses HyperFlow, a desktop app connected through DevtoolsPlugin. It shows requests, cache entries and queues, and lets you pause or resume a queue.


Offline and queues​

HyperFetch sends requests through dispatcher queues. Requests made offline wait for the connection by default (offline: true), queued: true sends them one by one, and queues can be paused, resumed and persisted. This is useful for React Native apps and forms that must not lose a submit. See offline and queueing.


React Query vs RTK Query​

If your choice is really between TanStack Query and RTK Query, the RTK docs put it well: with React Query and SWR "you usually define your hooks yourself", while RTK Query defines everything in one API slice. Use RTK Query in a Redux app, TanStack Query when there is no Redux store. Our HyperFetch vs TanStack Query page has the details on TanStack Query.


Side by side​

CapabilityRTK QueryHyperFetch
Requires ReduxYes (or ApiProvider without a store)No
API definitionOne createApi sliceSeparate createRequest objects
Default transportfetchBaseQuery, a fetch wrapperfetch adapter, Axios/GraphQL/Firebase adapters
InvalidationTagsRequest, key or RegExp
RealtimeonCacheEntryAdded, your own connection@hyper-fetch/sockets
CodegenOpenAPI, GraphQL (experimental)OpenAPI, Swagger
DevtoolsRedux DevToolsHyperFlow
Normalized cacheNo, by designNo

Which one fits you​

Pick RTK Query when:

  • Your app already uses Redux, and you want server data in the same store and DevTools.
  • Other Redux logic (middleware, listeners, reducers) needs to react to API calls.
  • You like one API slice file with tags describing what invalidates what.

Pick HyperFetch when:

  • You don't use Redux and don't want to add a store just for data fetching.
  • You need WebSockets or SSE as a first-class part of the API, not a lifecycle callback.
  • You want request queues, offline handling and upload progress without extra code.
  • You want each endpoint as its own typed object that works the same in React, Node and tests.
Summary
  • RTK Query is the natural choice in Redux apps and ships inside Redux Toolkit.
  • Both define the API up front and send requests themselves.
  • RTK Query invalidates with tags. HyperFetch invalidates by request, key or RegExp.
  • HyperFetch adds a socket package, request queues and offline handling, with no store.