Skip to content
Clean CodeChapter 9 / 57Intermediate5 min read

File Size & Organization

How to organize large components. A 400 line component limit, one responsibility per file, and splitting a big React file into child components.

On this page

1. What is this practice?

You saw function size in Small Functions. A file is one level up: it is the home for several functions and components. The question here is "should all of this really live in one place?"

PartMeaning
400 line limitThe upper bound for a .tsx component file
One responsibilityThe file name tells you what is inside, and only that is inside
One unit, one foldercomponents/LeadTable/LeadTable.tsx with its own parts next to it
One naming stylePascalCase or kebab-case within a folder, never a mix

2. Why does it matter?

In a 1200 line file, every change feels risky. You lose context while scrolling, and two developers editing the same file end up in a merge conflict.

One big fileSmall focused files
Merge conflict on almost every changeDifferent people edit different files, fewer conflicts
One state change re-renders the whole fileChild components manage their own state
Finding something means Ctrl F and scrollingThe file name tells you where to look
A 600 line diff in reviewA small diff and a focused review
Reusing one piece means importing the whole fileImport only the component you need

3. When to follow it

LevelHow importantWhy
Learning projectSplit when scrolling the file becomes painfulSplitting components is a core React skill
Production codeEnforce the 400 line limit in reviewBig files are the biggest enemy of team speed
Senior levelDecide the folder structure and boundariesWhere to split is a design decision

4. Bad vs good code

Bad: one Dashboard.tsx that is 900 lines long.

tsx
export function Dashboard() {
  const [filters, setFilters] = useState<Filters>(DEFAULT_FILTERS);
  const [selected, setSelected] = useState<string[]>([]);
  const { leads, isLoading } = useLeads(filters);

  return (
    <main>
      <header>
        <h1>Leads</h1>
        <input value={filters.query} onChange={(e) => setFilters({ ...filters, query: e.target.value })} />
        <select value={filters.status} onChange={(e) => setFilters({ ...filters, status: e.target.value as LeadStatus })}>
          <option value="all">All</option>
        </select>
      </header>
      <table>
        <tbody>
          {leads.map((lead) => (
            <tr key={lead.id}>
              <td>{lead.name}</td>
            </tr>
          ))}
        </tbody>
      </table>
      <footer>{selected.length} selected</footer>
    </main>
  );
}

This only shows the outline. In the real file each part is 200 lines: the filter logic, the table sorting, the bulk actions modal.

Good: each part in its own file.

bash
components/
  Dashboard/
    Dashboard.tsx
    DashboardFilters/
      DashboardFilters.tsx
    LeadTable/
      LeadTable.tsx
      LeadRow.tsx
    BulkActions/
      BulkActions.tsx
hooks/
  use-leads/
    use-leads.ts
tsx
export function Dashboard() {
  const [filters, setFilters] = useState<Filters>(DEFAULT_FILTERS);
  const [selected, setSelected] = useState<string[]>([]);
  const { leads, isLoading } = useLeads(filters);

  return (
    <main>
      <DashboardFilters filters={filters} onChange={setFilters} />
      <LeadTable leads={leads} isLoading={isLoading} onSelect={setSelected} />
      <BulkActions selected={selected} />
    </main>
  );
}
What is the difference?BadGood
Size900 lines50 to 150 lines per file
ResponsibilityFilters, table and actions all togetherDashboard only wires things together
Finding codeScrollingThe folder name
TestingRender the whole dashboardTest LeadTable alone
TeamworkEveryone in one fileEveryone in their own file

5. How to apply it

Steps to split a big file
Scroll through the file and spot its visual or logical parts
Any part that has its own state or its own data is a candidate
Move that part into its own file and folder
Pass only the props that part needs
Move the logic into a hook or a pure function
Let the parent only connect the children

Good signs that something should be split: separate state, a separate data source, a different reason to change, or a large block of JSX inside .map (that is a Row component).

6. Exceptions and common mistakes

400 lines is an upper limit, not a target. Sometimes splitting a file even hurts: if two parts always change together and depend on each other's internal details, separate files only add navigation. Keep things that change together in one place, and things that change separately apart.

Config files, generated types or long constant lists can be longer than 400 lines, because they contain no logic.

Mistake: A separate file for everything, even 5 lines

Why? 20 tiny files that only import each other make the code harder to read.

Better: Extract a part when it has its own responsibility or the file is truly getting big.

Mistake: Mixing naming styles in one folder

Why? With LeadTable.tsx in one place and lead-row.tsx in another, every import becomes a guess.

Better: Pick one convention per folder and keep it across the whole repo.

Mistake: Putting everything in utils.ts

Why? A utils file becomes everyone's dumping ground and can reach 1000 lines.

Better: Keep each helper next to the module that uses it, with a clear name.

Mistake: Keeping everything in one file to avoid prop drilling

Why? You lose the benefit of a short file, and the problem is still there.

Better: Use composition, the children prop or context, and still split the file.

7. What to learn before and after

Final quick revision

CheckQuestion
SizeIs the component file under 400 lines?
ResponsibilityDoes the file name describe everything inside it?
FolderDoes each component live in its own folder?
NamingDoes the folder use one naming style?
CohesionAre things that change together kept together?