Parallel requests
A dashboard with three cards: users, posts and todos. Each card has its own request, and the API answers them with different delays, so you can watch the cards fill in one by one while a global indicator counts what is still loading.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { createClient } from "@hyper-fetch/core";
export const client = createClient({ url: "https://dummyjson.com" });
/** dummyjson waits `delay` ms before it answers, so every card finishes at a different moment. */
type ListQuery = { limit: number; select: string; delay: number };
export type User = { id: number; firstName: string; lastName: string };
export type Post = { id: number; title: string };
export type Todo = { id: number; todo: string; completed: boolean };
export const getUsers = client.createRequest<{
response: { users: User[]; total: number };
queryParams: ListQuery;
}>()({ endpoint: "/users" });
export const getPosts = client.createRequest<{
response: { posts: Post[]; total: number };
queryParams: ListQuery;
}>()({ endpoint: "/posts" });
export const getTodos = client.createRequest<{
response: { todos: Todo[]; total: number };
queryParams: ListQuery;
}>()({ endpoint: "/todos" });
npx tiged BetterTyped/hyper-fetch/examples/react/parallel-requests hyper-fetch-react-parallel-requests && cd hyper-fetch-react-parallel-requests && npm install && npm run dev- Parallel by default: several
useFetchcalls in one component start together, with no waterfall. - Independent states: every card reads its own
loading,erroranddata, so a slow request never blocks a fast one. - Global indicator:
useIsFetching(client)returns how many fetch requests are running right now. - Refresh one or all:
refetch()sends one request again; calling it on every hook refreshes the whole screen.
How it works
-
api.tsdefinesgetUsers,getPostsandgetTodos. TheirqueryParamsincludedelay, which dummyjson uses to wait before it answers. The example sets 600, 1400 and 2200 ms so the order is easy to see. -
AppcallsuseFetchthree times. Hooks do not wait for each other: each one adds its request to the client's fetch dispatcher on mount, and the browser sends all three at once.const users = useFetch(getUsers.setQueryParams({limit: 3,// `select` and `delay` are dummyjson query params: return only these fields, answer after `delay` ms.select: "firstName,lastName",delay: 600,}),);const posts = useFetch(getPosts.setQueryParams({ limit: 3, select: "title", delay: 1400 })); -
Cardgets theloadinganderrorof one hook only. Show a placeholder per section instead of one spinner for the whole page. -
GlobalIndicatorcallsuseIsFetching(client). It re-renders only when the number of running requests changes, so it is cheap to keep in a header or a top bar. -
Refresh all calls
refetch()on each hook. A manualrefetch()always goes to the network, even when the cached data is still fresh.
- Dashboards and overview pages built from several unrelated endpoints.
- Any screen where one slow endpoint should not hold back the rest.
- A global "loading" bar or favicon badge driven by
useIsFetching(oruseIsSubmittingfor saves).
Related
Each useFetch call is an independent request with its own state, and several of them run in parallel. useIsFetching
gives you one number for everything still in flight.
