Dependent requests
A user profile that is loaded by username. The posts and todos endpoints need the numeric user id, which only the first response contains, so those two requests wait for it. A small step list shows each request waiting, loading or done.
beginneruseFetchparams
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { useFetch } from "@hyper-fetch/react";
import { useState } from "react";
import { findUser, getUserPosts, getUserTodos } from "./api";
const usernames = ["emilys", "michaelw", "sophiab", "oliviaw", "ghost"];
type StepProps = { label: string; disabled: boolean; loading: boolean; failed: boolean };
/** Shows where each request is in the chain. */
const Step = ({ label, disabled, loading, failed }: StepProps) => {
let state = "done";
if (disabled) state = "waiting";
else if (loading) state = "loading";
else if (failed) state = "failed";
return (
<li>
<code>{label}</code> <mark className={`step ${state}`}>{state}</mark>
</li>
);
};
export const App = () => {
const [username, setUsername] = useState(usernames[0]);
// "clean" drops the previous user while the next one loads, so the steps below go back to waiting.
const user = useFetch(
// `select` and `delay` are dummyjson query params: return only these fields, answer after `delay` ms.
findUser.setQueryParams({ key: "username", value: username, select: "firstName,lastName,email", delay: 700 }),
{ keepPreviousData: "clean" },
);
const userId = user.data?.users[0]?.id;
// The params need a value even while disabled. Nothing is sent until `disabled` turns false.
const posts = useFetch(getUserPosts.setParams({ userId: userId ?? 0 }), { disabled: !userId });
const todos = useFetch(getUserTodos.setParams({ userId: userId ?? 0 }), { disabled: !userId });
const profile = user.data?.users[0];
return (
<main className="container">
<hgroup>
<h1>Dependent requests</h1>
<p>Posts and todos need the user id, so they wait until the user lookup returns it.</p>
</hgroup>
<div role="group">
{usernames.map((name) => (
<button
key={name}
type="button"
className={name === username ? undefined : "outline secondary"}
onClick={() => setUsername(name)}
>
{name}
</button>
))}
</div>
<ol className="steps">
<Step label="GET /users/filter" disabled={false} loading={user.loading} failed={!!user.error} />
<Step label="GET /posts/user/:userId" disabled={!userId} loading={posts.loading} failed={!!posts.error} />
<Step label="GET /todos/user/:userId" disabled={!userId} loading={todos.loading} failed={!!todos.error} />
</ol>
{user.loading && <article aria-busy="true">Looking up {username}...</article>}
{user.error && <article>Could not load the user: {user.error.message}</article>}
{user.data && !profile && <article>No user is called "{username}", so nothing else is fetched.</article>}
{profile && (
<article>
<header>
<h3>
{profile.firstName} {profile.lastName}
</h3>
<small className="muted">{profile.email}</small>
</header>
<div className="grid-2">
<section>
<h6>Posts</h6>
{posts.loading && <p aria-busy="true">Loading posts...</p>}
{posts.error && <p>Could not load posts: {posts.error.message}</p>}
{posts.data?.posts.length === 0 && <p className="muted">No posts.</p>}
<ul>
{posts.data?.posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</section>
<section>
<h6>Todos</h6>
{todos.loading && <p aria-busy="true">Loading todos...</p>}
{todos.error && <p>Could not load todos: {todos.error.message}</p>}
{todos.data?.todos.length === 0 && <p className="muted">No todos.</p>}
<ul>
{todos.data?.todos.map((todo) => (
<li key={todo.id}>
{todo.completed ? "✓" : "○"} {todo.todo}
</li>
))}
</ul>
</section>
</div>
</article>
)}
</main>
);
};
Run locally
npx tiged BetterTyped/hyper-fetch/examples/react/dependent-requests hyper-fetch-react-dependent-requests && cd hyper-fetch-react-dependent-requests && npm install && npm run devWhat it shows
disabledkeeps auseFetchrequest from being sent until its input is known.- Automatic start. When
disabledturnsfalse, the hook sends the request on its own. - Parallel follow-ups. Posts and todos both depend on the user and load at the same time.
keepPreviousData: "clean"clears the previous user when you switch, so the chain starts over visibly.
How it works
api.tsdefinesfindUser, which searches/users/filterby username, and two requests that take:userId:getUserPostsandgetUserTodos. Thedelayquery param slows dummyjson down so the waiting state is visible.AppcallsuseFetch(findUser.setQueryParams(...))and readsuserIdfrom the first matching user.- The posts and todos hooks pass
{ disabled: !userId }. While it istrue, nothing is sent. The params still need a value, so the example passesuserId ?? 0as a placeholder. - When the user response arrives,
userIdis set,disabledturnsfalseand both requests start in parallel. - The
ghostusername returns no user, so the two dependent requests stay disabled and never reach the network.
const userId = user.data?.users[0]?.id;
const posts = useFetch(getUserPosts.setParams({ userId: userId ?? 0 }), { disabled: !userId });
By default (keepPreviousData: "auto") the hook keeps showing the previous result while only query params change. The
user lookup sets "clean" instead, so switching users drops the old profile and its posts right away.
When to use it
- A request needs an id, token or setting that another request returns.
- Details that load only after the user picks something, such as a selected row or a form value.
Related
Summary
You chained requests with the disabled option: the follow-up requests wait until the first response provides the user
id, then start on their own and run in parallel.
