Basic data fetching
A list of posts with a details panel. It is the smallest complete HyperFetch app: one client, three typed requests and
the useFetch hook.
beginneruseFetchparamscachetypescript
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 { getPost, getPostComments, getPosts } from "./api";
const PostList = ({ selected, onSelect }: { selected: number | null; onSelect: (id: number) => void }) => {
// The request is sent on mount. Data, error and loading state come from the shared cache.
// `select` is a dummyjson query param: the API returns only these fields.
const { data, error, loading, refetch } = useFetch(getPosts.setQueryParams({ limit: 10, select: "title" }));
if (loading && !data) return <p aria-busy="true">Loading posts...</p>;
if (error) return <p>Could not load posts: {error.message}</p>;
return (
<section>
<header className="toolbar muted">
<span>
Showing {data?.posts.length} of {data?.total} posts
</span>
<button type="button" className="outline secondary" onClick={refetch} disabled={loading}>
Refresh
</button>
</header>
<ul className="list">
{data?.posts.map((post) => (
<li key={post.id}>
<button
type="button"
className={post.id === selected ? undefined : "outline secondary"}
onClick={() => onSelect(post.id)}
>
{post.title}
</button>
</li>
))}
</ul>
</section>
);
};
const PostDetails = ({ postId }: { postId: number }) => {
// Changing `postId` creates a new cache key, so every post is fetched once and then served from cache.
const post = useFetch(getPost.setParams({ postId }));
const comments = useFetch(getPostComments.setParams({ postId }));
if (post.loading && !post.data) return <article aria-busy="true">Loading post...</article>;
if (post.error) return <article>Could not load the post: {post.error.message}</article>;
if (!post.data) return null;
return (
<article>
<header>
<h3>{post.data.title}</h3>
<small className="muted">
{post.data.views} views · {post.data.reactions.likes} likes · {post.data.tags.join(", ")}
</small>
</header>
<p>{post.data.body}</p>
<footer>
<h6>Comments</h6>
{comments.loading && <p aria-busy="true">Loading comments...</p>}
{comments.data?.comments.map((comment) => (
<p key={comment.id}>
<strong>@{comment.user.username}</strong> {comment.body}
</p>
))}
</footer>
</article>
);
};
export const App = () => {
const [selected, setSelected] = useState<number | null>(null);
return (
<main className="container">
<hgroup>
<h1>Posts</h1>
<p>Pick a post. Posts you already opened load instantly from the cache.</p>
</hgroup>
<div className="grid-2">
<PostList selected={selected} onSelect={setSelected} />
{selected ? <PostDetails postId={selected} /> : <p className="muted">No post selected.</p>}
</div>
</main>
);
};
Run locally
npx tiged BetterTyped/hyper-fetch/examples/react/basic hyper-fetch-react-basic && cd hyper-fetch-react-basic && npm install && npm run devWhat it shows
- One client holds the base URL that every request uses.
- Typed requests describe the response, the endpoint params (
:postId) and the query params once. useFetchsends a request on mount and returnsdata,errorandloading.- Shared cache makes a post you already opened show up instantly the second time.
How it works
api.tscreates the client withcreateClient({ url })and defines the requests withclient.createRequest(). The generic argument sets the response type, andqueryParamstypessetQueryParams().PostListcallsuseFetch(getPosts.setQueryParams(...)). The request is sent when the component mounts, andrefetch()sends it again on demand.PostDetailscallsgetPost.setParams({ postId }). EverypostIdgets its own cache key, so switching back to a post reads it from the cache instead of the network.- Two
useFetchcalls in one component run in parallel, so the post and its comments load at the same time.
When to use it
- Any screen that reads data when it opens.
- Lists with a details view, where users switch back and forth.
Related
Summary
You created a client, typed three requests and read them with useFetch. The cache keys built from params let
HyperFetch reuse data between components and visits.
