Managing Complexity
As your projects grow, you'll need to organize code across multiple files. This isn't just about cleanliness ā it helps Claude understand and modify your code more effectively.
šÆ What You'll Learn
- ⢠When to split code into multiple files
- ⢠Common folder structures
- ⢠Components, utilities, and hooks
- ⢠Import/export patterns
Standard Next.js Structure
my-app/ āāā app/ # Pages and routes ā āāā page.js # Homepage ā āāā about/page.js # /about page ā āāā layout.js # Shared layout āāā components/ # Reusable UI pieces ā āāā Header.js ā āāā Footer.js ā āāā Button.js āāā lib/ # Utilities and helpers ā āāā utils.js ā āāā supabase.js āāā public/ # Static files (images)
When to Create a New File
Reusable Component
If you use the same UI element in multiple places, extract it to components/
Utility Function
Helper functions used across files go in lib/utils.js
New Page
Each route gets its own folder in app/ with a page.js file
š” Ask Claude
"This file is getting long. Can you refactor it by extracting the [component name] into its own file in the components folder?"