-
Notifications
You must be signed in to change notification settings - Fork 7
React components and hooks
Summary: This page documents how data reactivity works in StartER, leveraging React's cache system and the standard useMutate hook.
Routing is handled by React Router. Each page is a React component associated with a path in src/react/routes.tsx. StartER uses a modern approach based on Promises and caching to manage data loading.
StartER separates data retrieval (Reading) from data modification (Writing).
To fetch data, we advise to use React's use function with the cache utility function of StartER. These functions ensure that the same request is not sent repeatedly during re-renders.
Example in ItemList.tsx:
import { use } from "react";
import { cache } from "../../helpers/cache";
function ItemList() {
// Suspends the component until data arrives
const items = use(cache<Item[]>("/api/items"));
return (
<ul>
{items.map(item => <li key={item.id}>{item.title}</li>)}
</ul>
);
}The challenge for "data-driven" React applications is updating the interface after a modification (mutation). StartER solves this via DataRefreshContext and the useMutate hook.
-
Mutation: API call (POST/PUT/DELETE) via the
apiMutate()function. -
Invalidation: removal of stale data from the cache via
invalidateCache(). You can also clear the entire cache by usinginvalidateCache("*"). - Refresh: triggering a global refresh via a "tick" (counter) that forces React to re-suspend components and thus re-fetch fresh data.
The useMutate hook encapsulates these three steps:
// src/react/components/item/ItemCreate.tsx
const mutate = useMutate();
const addItem = async (partialItem) => {
await mutate(
"/api/items", // URL
"post", // Method
partialItem, // Request body
["/api/items"] // Paths to invalidate in cache
);
// Redirect or success message
};For this system to work, the application must be wrapped in a <DataRefreshProvider>. This is already configured in src/react/routes.tsx.
It provides a "global signaling" mechanism: as soon as a mutation succeeds, all components using cached (and invalidated) data will automatically update to display the most recent data.
-
Atomic components: keep your UI components "pure". They should rely on
use(cache(...))for reading anduseMutate()for writing. -
Explicit invalidation: don't forget to pass the list of affected paths to
useMutateso the user doesn't see stale data.
AI co-creation
Getting started
Explanations
How-To Guides
Reference
Digging deeper