Course contentsShow
← React
Lesson 62 of 2,3962. Setup and Modern ToolingFree lesson

Colocation: Keeping Related Files Together

Learn the principle of placing tests, styles, and types next to the components that use them for easier maintenance.

Colocation: Keeping Related Files Together

What you'll learn: How grouping component files with their tests, styles, and types makes your codebase easier to navigate and maintain.

The Core Idea

Colocation means placing files that change together next to each other, rather than scattering them across different folders by file type. Instead of having all tests in a tests/ folder and all styles in a styles/ folder, you keep each component's related files in the same directory.

Why This Matters

Think of it like organizing a toolbox: would you rather store all screwdrivers in one drawer, all hammers in another, and all nails in a third? Or keep each project's tools together in their own compartment? Colocation is the "project compartment" approach.

When you're working on a Button component, you immediately need its styles and tests. Walking through deep folder hierarchies wastes time and mental energy.

What Gets Colocated

Typical files living together:

Button/
  Button.jsx          # The component
  Button.module.css   # Component-specific styles
  Button.test.jsx     # Tests for this component
  Button.types.ts     # TypeScript types (if using TS)
  index.js            # Optional: clean exports

Everything about Button lives in the Button/ folder. Delete the folder, and you've removed all related code—no orphaned test files hiding elsewhere.

The Practical Benefit

This pairs perfectly with feature-based organization from the previous lesson. Your src/features/auth/LoginForm/ folder contains the form component, its validation logic, styles, and tests—all the "login form stuff" in one place.

Key Takeaway: Colocation reduces cognitive overhead by keeping files that change together physically close, making features self-contained and easier to understand, modify, or delete.