One Component Per File (Usually)
You'll learn: Why keeping one component per file improves maintainability and when it's okay to break this rule.
The Standard: One File, One Component
Just like you wouldn't cram an entire book into a single chapter, you typically shouldn't pack multiple unrelated components into one file. The React community strongly favors one primary component per file, and for good reason.
Why This Matters
When each component lives in its own file:
- You can find things quickly. Need to update
UserProfile? OpenUserProfile.jsx. Simple. - Import statements become self-documenting. When you see
import UserProfile from './UserProfile', you know exactly what you're getting. - Merge conflicts decrease. Multiple developers can work on different components without editing the same file.
- Your editor's file search actually works. Type "UserProfile" and jump straight to it.
Think of it like organizing a toolbox: each tool gets its own compartment, not all jumbled together.
When to Break the Rule
Sometimes keeping related components together makes sense:
- Small, private helper components used only by one parent component
- Tightly coupled components that always appear together
- Very simple components (2-3 lines) that exist purely to support one main component
For example, if you have a UserCard component with a tiny UserCardHeader that's never used anywhere else, keeping them in the same file is perfectly reasonable.
The Practical Approach
Start with one component per file. If you notice a helper component is only used in one place and moving it to a separate file feels like overkill, keep it local. But if any component grows beyond ~20 lines or gets reused elsewhere, give it its own file.
Key Takeaway: One component per file is the default for good reason—it makes your codebase navigable and maintainable. Break this rule thoughtfully, not lazily.