Sub topic 7
Layering pages, feature components, and shared UI
This chapter assumes you can write React components and hooks, know TypeScript import and export syntax, and have already seen the feature-module layout from the earlier chapters. Here you will split components into three layers and set the rules for how they may depend on each other.
The three layers
Components in a large codebase do different jobs. Mixing those jobs in one folder makes dependencies hard to follow. Three layers cover most cases.
| Layer | Lives in | Job | Knows about |
|---|---|---|---|
| Page | `app/` or `pages/` | Maps a route to a screen and arranges features | Features, shared UI |
| Feature component | `features/<name>/components/` | Implements one piece of business behavior | Its own feature, shared UI |
| Shared UI | `shared/ui/` | Generic building blocks such as buttons and inputs | Nothing about the business |
The dependency rule
Imports point in one direction only: pages import features, and features import shared UI. Shared UI imports neither. A feature never imports a page.
Shared UI stays reusable because it knows nothing about carts or orders. If a button imports useCart, it can no longer be used outside the cart feature.
A worked example
Start with shared UI. It receives everything through props and holds no business logic.
// src/shared/ui/Button.tsx
import type { ButtonHTMLAttributes } from "react";
export function Button(props: ButtonHTMLAttributes<HTMLButtonElement>) {
return <button type="button" {...props} />;
}A feature component combines shared UI with the feature's own hook. It owns the behavior.
// src/features/cart/components/ClearCartButton.tsx
import { Button } from "@/shared/ui/Button";
import { useCart } from "../hooks/useCart";
export function ClearCartButton() {
const { items, clear } = useCart();
return (
<Button disabled={items.length === 0} onClick={clear}>
Clear cart
</Button>
);
}A page picks features and arranges them. It holds little logic of its own.
// src/app/pages/CheckoutPage.tsx
import { CartList, ClearCartButton } from "@/features/cart";
import { OrderSummary } from "@/features/orders";
export function CheckoutPage() {
return (
<main>
<h1>Checkout</h1>
<CartList />
<ClearCartButton />
<OrderSummary />
</main>
);
}This assumes the cart and orders index files export those names, as described in the public API chapter. The page imports only from index files.
Common errors
- A feature imports another feature's internals. Import from its index file instead. If two features need the same code often, move that code to
shared/. - Shared UI fetches data. A
shared/uicomponent that calls an API is really a feature component. Move it, or pass the data in as props. - Pages hold business logic. If a page has many
useStatecalls and handlers, move them into a feature hook or component. - A shared folder that grows without limit. Put code in
shared/only after at least two features need it.
Choosing a layer
Ask these questions in order:
- Does it know about a business concept such as a cart or an order? If yes, it belongs in a feature.
- Does it map to a URL? If yes, it is a page.
- Is it generic, with no business knowledge? Then it is shared UI.
Exercise
A UserAvatar component in shared/ui imports useCurrentUser from features/auth to show the signed-in user's photo. Which rule does this break, and how would you fix it?
Show answer
It breaks the rule that shared UI must not depend on features. Make UserAvatar accept src and name as props, so it stays generic. Then create a feature component in features/auth/components, for example CurrentUserAvatar, that calls useCurrentUser and renders UserAvatar with those props.