Component Boundaries and React Project Structure
Organize React and TypeScript by feature, separate reusable UI from domain logic, and spot when a component has grown beyond one job.
Feature-first organization keeps React and TypeScript modules close to the UI and data behavior that change together. The examples separate a task row from request logic, show when a component deserves its own boundary, and clarify how imports should flow. Use them to keep features easy to find without turning every small view into an abstraction.
Component Boundaries and React Project Structure
Introduction
React gets harder to change when components, API calls, and types pile into one folder. Organize code so a feature is easy to find and shared pieces stay reusable.
This guide groups modules by feature, separates generic UI from domain behavior, and identifies signs that a component needs a clearer boundary.
Group code by feature
Keep code that changes together in the same feature folder. Shared components stay neutral and can sit beside features:
src/
components/
Button.tsx
Spinner.tsx
features/
tasks/
TaskPage.tsx
TaskRow.tsx
useTasks.ts
taskApi.ts
types.ts
A task page can own its request hook and API module. Promote a component to components/ when more than one feature uses the same behavior and props.
Separate reusable UI and domain behavior
Reusable UI accepts data and callbacks without knowing their source. Domain components can understand task concepts. Move fetching into a hook or API module when that logic grows.
// features/tasks/TaskRow.tsx
interface TaskRowProps {
title: string;
done: boolean;
onToggle: () => void;
}
export function TaskRow({ title, done, onToggle }: TaskRowProps) {
return (
<button onClick={onToggle}>
{done ? "✓ " : ""}
{title}
</button>
);
}
The row does not fetch data or know about routes. The task feature adapts a Task model to its props, keeping data handling out of the row component.
Keep modules discoverable
Use descriptive names such as TaskRow.tsx and useTasks.ts. Keep types near their feature unless they are shared. Avoid a catch-all utils.ts; local ownership makes search easier.
flowchart LR
A[Task page] --> B[Task feature hook]
B --> C[Task API module]
A --> D[Task domain components]
D --> E[Shared UI components]
Imports should flow from page to feature to shared utilities. A shared button should not import task data.
Test the boundary
A focused component test can verify that TaskRow renders its public props and delegates an interaction. It does not need to know how the page loads tasks or which hook stores them.
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { expect, it, vi } from "vitest";
import { TaskRow } from "./TaskRow";
it("renders task data and calls the supplied toggle", async () => {
const user = userEvent.setup();
const onToggle = vi.fn();
render(<TaskRow title="Write docs" done={false} onToggle={onToggle} />);
await user.click(screen.getByRole("button", { name: /write docs/i }));
expect(onToggle).toHaveBeenCalledOnce();
});
The assertion protects the component contract. It does not test the feature hook, network request, or full page flow; cover those at the boundary where they are owned.
When to Use / When NOT to Use
Split components when they own unrelated jobs, repeat UI, or are hard to follow across several states. Keep a small, cohesive view together; length alone is not a reason to split it.
Trade-Off Table
| Structure | Works well when | Cost |
|---|---|---|
| Group by file type | Tiny app with few screens | Related files spread across folders |
| Group by feature | Features have distinct behavior and UI | Shared code needs a clear home |
| Shared UI folder | Several features use the same primitive | Too much abstraction for one caller |
| One large component | Quick experiment or simple view | Becomes difficult to navigate as jobs accumulate |
Production Failure Scenarios
If every TaskRow fetches its own task, a list of ten rows can make ten requests and the row is hard to reuse in a preview. Fetch in the feature hook, then pass each row its data and callback.
If a shared Button imports Task types or task styles, other features inherit that dependency and a task refactor can break unrelated screens. Keep shared primitives domain-neutral and map feature data to simple props.
Common Pitfalls and Anti-Patterns
Avoid splitting every element, centralizing all types, or making shared components depend on a feature. If unrelated screens keep changing together in one component, separate the responsibilities.
Observability Checklist
- Track feature request failures at the data boundary.
- Use module and component names that make errors traceable.
Security and Compliance Notes
Keep credentials out of client modules and logs. Validate external data before passing it through typed props. TypeScript types disappear at runtime. A clean folder layout does not validate API responses or protect secrets, so keep runtime checks and access rules at the data boundary.
Quick Recap Checklist
- Group related UI and behavior by feature.
- Keep reusable primitives independent of domain data.
- Put request logic in a feature hook or API module when it grows.
- Split components when their jobs or change patterns diverge.
- Keep names and imports pointed toward clear ownership.
Interview Questions
- A task feature can keep its list, API module, and hook nearby.
- Shared primitives belong outside when several features use them.
- Keep feature data mapping and fetching outside the primitive.
- This lets more than one feature use the same UI.
- Look for independent responsibilities or repeated UI.
- Split when the boundary improves understanding and change ownership.
- Pass results into display components as typed props.
- Keep network work out of repeated row primitives.
- Name modules after the behavior or feature they own.
- Keep a helper near its only caller until it is shared.
- A shared primitive should not import domain data.
- Clear direction reduces circular and accidental dependencies.
- Check external values at the feature data boundary.
- Keep credentials and access rules outside client modules.
Further Reading
-
React props, children, and composition shows how components share data and behavior through their interfaces.
-
Component and interaction testing covers testing reusable UI at its visible boundaries.
-
Sharing state and lifting state up explains how related components can share one state owner.
-
Read React’s guide to thinking in React and TypeScript’s guide to modules.
Conclusion
Keep a component inside its feature while its props and behavior belong to that domain. Move it to shared UI only when another feature can use the same interface without importing feature-specific types or rules.
Category
Related Posts
React Props, Children, and Component Composition
Learn to pass data and callbacks through React props, use children for flexible layouts, and compose small components without building giant prop APIs.
Typed Function Components in React and TypeScript
Learn to type React function component props, understand inferred return types, handle optional values safely, and compose small components with confidence.
Accessible React UI: Semantics, Labels, and Keyboard Use
Build React interfaces with semantic controls, connected labels, visible keyboard focus, and status announcements through practical TypeScript examples.