Sub topic 3
A Data Access Layer and Server State
Assume you know React components, hooks, and import/export syntax. You should also be familiar with npm and a bundler such as Vite or webpack.
A data access layer and server state
Keep network requests in a feature’s api.ts file. A custom hook can then connect those requests to a server-state library such as TanStack Query. Server state is data owned by a backend, so it needs caching, loading status, refetching, and error handling.
// src/features/orders/api.ts
export type Order = { id: string; total: number };
export async function fetchOrders(userId: string): Promise<Order[]> {
const response = await fetch(`/api/users/${encodeURIComponent(userId)}/orders`);
if (!response.ok) throw new Error("Could not load orders");
return response.json();
}
// src/features/orders/hooks/useOrders.ts
import { useQuery } from "@tanstack/react-query";
import { fetchOrders } from "../api";
export function useOrders(userId: string) {
return useQuery({
queryKey: ["orders", userId],
queryFn: () => fetchOrders(userId),
});
}This keeps components focused on rendering. Use component state for local display details, feature state for workflows shared within one feature, and server state for backend data.
Shared UI and design systems
Place generic components such as Button, Modal, and TextField in shared/ui. A shared component should not know about orders, users, or other business concepts. Feature components compose shared UI with feature-specific behavior.
Enforcing boundaries with ESLint
Define each feature’s public API in index.ts. Other features import from that file instead of reaching into internal folders. An ESLint no-restricted-imports rule can reject imports such as @/features/cart/components/CartList.
Where tests live and what they cover
Colocate tests with the code they test. Test shared UI for rendering and interaction, hooks for state transitions, and feature components for user-visible behavior. Keep API tests focused on request formatting and response handling.
Splitting code by route
Use route-based code splitting when a page is not needed immediately. React.lazy loads a module when its route is visited, while Suspense displays a fallback during loading.
Migrating step by step
Start with one actively changing feature. Move its files, add an index.ts, update imports, and add boundary checks. Repeat feature by feature. Avoid a repository-wide rename that creates a large, hard-to-review change.
When to use monorepo packages
Use workspace packages when separate applications need independently owned code, such as @company/ui or @company/api-client. Do not create packages merely to add folders. A package adds versioning, build, and dependency decisions.
| Situation | Prefer |
|---|---|
| One application with related features | Feature folders |
| Several applications sharing stable code | Workspace packages |
Exercise
Where should a request for order data live?
Show answer
In the orders feature's data access module, exposed through a custom hook rather than called directly by a component.