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.

published: reading time: 7 min read author: Geek Workbench
Quick Summary

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

1. Why organize a React app by feature?
Related UI, types, and data behavior stay close, making one feature easier to find and change.
2. What belongs in a reusable UI component?
Typed data and callbacks for display and interaction. Keep feature fetching and domain decisions in the feature layer.
3. When is a component ready to split?
Split unrelated responsibilities or repeated sections that can change independently. File length alone is not enough.
4. What is a feature-first folder structure?
It groups a feature's components, types, and data logic together.
  • A task feature can keep its list, API module, and hook nearby.
  • Shared primitives belong outside when several features use them.
5. What should a reusable UI primitive know about?
It should know how to display its props and report actions through callbacks.
  • Keep feature data mapping and fetching outside the primitive.
  • This lets more than one feature use the same UI.
6. Is line count enough reason to split a component?
No. A short component can have several unrelated jobs, while a longer view can remain cohesive.
  • Look for independent responsibilities or repeated UI.
  • Split when the boundary improves understanding and change ownership.
7. Where should feature request logic live?
Keep it near the feature, in a hook or API module as it grows.
  • Pass results into display components as typed props.
  • Keep network work out of repeated row primitives.
8. Why avoid a catch-all utils module?
It hides ownership and makes unrelated helpers accumulate in one place.
  • Name modules after the behavior or feature they own.
  • Keep a helper near its only caller until it is shared.
9. What direction should imports follow?
Let pages depend on features, and features depend on neutral shared utilities.
  • A shared primitive should not import domain data.
  • Clear direction reduces circular and accidental dependencies.
10. Do folder boundaries validate runtime data?
No. Organization makes ownership clearer but does not validate responses.
  • Check external values at the feature data boundary.
  • Keep credentials and access rules outside client modules.

Further Reading

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.

#react #typescript #components

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.

#typescript #react #components

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.

#react #typescript #accessibility