Initial data
A product list with a details panel. The details request is slow on purpose, but the panel does not wait for it: it shows the product from the list right away and swaps in the server response when it arrives. A product that is not in the list has nothing to start from, so it shows a spinner instead.
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" });
export type Product = {
id: number;
title: string;
price: number;
thumbnail: string;
description: string;
rating: number;
stock: number;
category: string;
};
/** Both requests select the same fields, so a list item has the full shape of the details response. */
const select = "title,price,thumbnail,description,rating,stock,category";
export const getProductsRequest = client
.createRequest<{
response: { products: Product[] };
queryParams: { limit: number; select: string };
}>()({
endpoint: "/products",
})
.setQueryParams({ limit: 6, select });
/** `delay` keeps the details request slow, so you can see the initial data before the response replaces it. */
export const getProductRequest = client
.createRequest<{
response: Product;
queryParams: { select: string; delay: number };
}>()({
endpoint: "/products/:productId",
})
.setQueryParams({ select, delay: 1500 });
npx tiged BetterTyped/hyper-fetch/examples/react/initial-data hyper-fetch-react-initial-data && cd hyper-fetch-react-initial-data && npm install && npm run devinitialResponse: data auseFetchhook shows while the cache is still empty.- The request still runs: the response replaces the initial data and goes into the cache.
- Reusing data you already have: the list response is used as the starting point for the details.
How it works
-
api.tsselects the same fields for the list and the details, so a list item has the fullProductshape. The details request addsdelay: 1500to make the wait visible. -
Appfinds the selected product in the list data and passes it toProductDetailsasplaceholder. -
ProductDetailshands it touseFetchas the initial response:const { data, loading } = useFetch(getProductRequest.setParams({ productId }), {initialResponse: placeholder ? { data: placeholder } : null,});datais available on the first render, andloadingstaystrueuntil the server answers. -
The initial response is not written to the cache. It is used only when the cache has no entry for the key; once the product was fetched, the cached response is shown instead.
-
key={selected}remounts the panel for every product, so each one starts from its own initial data.
- A details view opened from a list that already has the needed fields.
- Data rendered on the server or bundled with the app, shown before the client request finishes.
- Not for data that is only partly known:
initialResponse.datamust match the response type.
Related
initialResponse gives useFetch something to show before the first response. The request still runs, and its response
replaces the initial data and fills the cache.
