Project Structure & Best Practices
A well-organized project structure is essential for React applications to remain maintainable as they grow. This guide covers practical folder organization patterns and naming conventions.
Standard React Project Structure
Section titled “Standard React Project Structure”Basic Structure
Section titled “Basic Structure”Every React project should start with this foundational structure:
Directorysrc/
Directorycomponents/
- …
Directoryhooks/
- …
Directorycontexts/
- …
Directoryservices/
- …
Directorypages/
- …
Directoryassets/
- …
Directoryutils/
- …
- App.js
- index.js
Folder Purposes
Section titled “Folder Purposes”- components/: Reusable UI components
- hooks/: Custom React hooks
- contexts/: React Context providers
- services/: API calls and external service integrations
- pages/: Top-level page components (for routing)
- assets/: Images, icons, fonts, and static files
- utils/: Helper functions and utilities
Structuring Approaches
Section titled “Structuring Approaches”Layer-Based Structure
Section titled “Layer-Based Structure”Organize by technical layers (recommended for most projects):
Directorysrc/
Directorycomponents/
Directoryui/
- Button.jsx
- Input.jsx
- Modal.jsx
Directorylayout/
- Header.jsx
- Sidebar.jsx
- Footer.jsx
Directoryforms/
- LoginForm.jsx
- SignupForm.jsx
Directoryhooks/
- useAuth.js
- useLocalStorage.js
Directoryservices/
- api.js
- authService.js
Directorycontexts/
- AuthContext.js
- ThemeContext.js
Advantages:
- Clear separation of concerns
- Easy to find components by type
- Good for teams with different skill levels
- Scales well for medium projects
Best for:
- Teams new to React
- Projects with clear UI component libraries
- When components are highly reusable
Feature-Based Structure
Section titled “Feature-Based Structure”Organize by business features (for complex applications):
Directorysrc/
Directoryfeatures/
Directoryauth/
Directorycomponents/
DirectoryLoginForm/
- …
DirectorySignupForm/
- …
Directoryhooks/
- useAuth.js
Directoryservices/
- authService.js
- index.js
Directorydashboard/
Directorycomponents/
DirectoryDashboardCard/
- …
DirectoryStatsWidget/
- …
Directoryhooks/
- useDashboard.js
Directoryservices/
- dashboardService.js
Directoryprofile/
Directorycomponents/
DirectoryProfileForm/
- …
DirectoryAvatarUpload/
- …
Directoryhooks/
- useProfile.js
Directoryshared/
Directorycomponents/
- Button.jsx
- Input.jsx
Directoryhooks/
- …
Directoryutils/
- …
Advantages:
- Features are self-contained
- Easy to understand business logic
- Good for large teams
- Supports micro-frontend architecture
Best for:
- Large applications (50+ components)
- Multiple development teams
- Complex business domains
- When features have little overlap
File Naming Conventions
Section titled “File Naming Conventions”Component Files
Section titled “Component Files”# PascalCase for component filesUserProfile.jsxNavigationMenu.jsxProductCard.jsx
# Include component type in folder structurecomponents/forms/LoginForm.jsxcomponents/ui/Button.jsxcomponents/layout/Header.jsx
Import Aliases Setup
Section titled “Import Aliases Setup”Vite Configuration
Section titled “Vite Configuration”// vite.config.jsimport { defineConfig } from 'vite';import react from '@vitejs/plugin-react';import path from 'path';
export default defineConfig({ plugins: [react()], resolve: { alias: { '@': path.resolve(__dirname, './src'), '@components': path.resolve(__dirname, './src/components'), '@hooks': path.resolve(__dirname, './src/hooks'), '@services': path.resolve(__dirname, './src/services'), '@utils': path.resolve(__dirname, './src/utils'), '@assets': path.resolve(__dirname, './src/assets'), '@pages': path.resolve(__dirname, './src/pages') } }});
Usage Examples
Section titled “Usage Examples”// Without aliasesimport Button from '../../../components/ui/Button/Button';import { useAuth } from '../../../hooks/useAuth';import { userService } from '../../../services/userService';
// With aliasesimport Button from '@components/ui/Button';import { useAuth } from '@hooks/useAuth';import { userService } from '@services/userService';
When to Refactor Structure
Section titled “When to Refactor Structure”Signs You Need Better Organization
Section titled “Signs You Need Better Organization”- Import Paths Getting Long:
../../../components/ui/Button
- Hard to Find Components: Spending time looking for files
- Naming Conflicts: Multiple components with similar names
- Large Folders: More than 10-15 files in a single folder
- Feature Coupling: Changes in one area break unrelated features
Migration Strategy
Section titled “Migration Strategy”- Start Small: Begin with one feature or component type
- Update Imports: Use find/replace for import paths
- Setup Aliases: Configure path aliases before big moves
- Test Thoroughly: Ensure nothing breaks during restructuring
- Document Changes: Update team on new conventions
Best Practices
Section titled “Best Practices”Component Organization
Section titled “Component Organization”- One Component Per File: Keep components focused and testable
- Colocate Related Files: Keep component, styles, and tests together
- Use Index Files: Simplify imports with index.js exports
- Consistent Naming: Stick to naming conventions across the project
Project Scaling
Section titled “Project Scaling”- Start Simple: Begin with flat structure, refactor as needed
- Feature Boundaries: Group related functionality together
- Shared Components: Extract reusable components to shared folder
- Regular Cleanup: Remove unused files and refactor regularly