Course contentsShow
← React
Lesson 202 of 2,3966. Component BasicsFree lesson

Co-locating Helper Components

When small, tightly-coupled helper components can live in the same file as their parent.

Co-locating Helper Components

You'll learn when it makes sense to keep small helper components in the same file as their parent component.

The Problem with Over-Splitting

You've learned that "one component per file" is the usual pattern. But sometimes you'll create tiny helper components that are only used by a single parent component — they're tightly coupled and never needed anywhere else.

Creating a separate file for every tiny helper can lead to unnecessary file sprawl and jumping between files just to understand one component's behavior.

When Co-location Makes Sense

Co-locating means keeping multiple components in the same file. This is acceptable when:

  • The helper component is only used by one parent (not reusable elsewhere)
  • It's small and simple (a few lines, not complex logic)
  • It's tightly coupled to the parent's behavior
  • Keeping it nearby makes the parent easier to understand

Think of it like kitchen prep: if you're making a recipe that needs a special garnish used nowhere else, you prepare it right there at your station. You don't set up a separate garnish station across the kitchen.

The Pattern

// HeaderIcon is only used by Header
function HeaderIcon({ icon }) {
  return <span className="header-icon">{icon}</span>;
}

// Main component uses the helper
export default function Header({ title, icon }) {
  return (
    <header>
      <HeaderIcon icon={icon} />
      <h1>{title}</h1>
    </header>
  );
}

Notice HeaderIcon lives in the same file because it's specific to Header and won't be used elsewhere.

Key Takeaway: Small, tightly-coupled helper components can live in the same file as their parent to reduce file sprawl and keep related code together — but only export the main component.