React/Next.js Project Structure

Overview

This document outlines a modern, scalable folder structure for React/Next.js applications. The structure follows feature-first architecture principles, promoting maintainability, scalability, and team collaboration.

Core Architecture Principles

1. Feature-First Design

  • Group related functionality together

  • Each feature is self-contained

  • Clear separation of concerns

2. Separation of Concerns

  • UI components vs business logic

  • Shared utilities vs feature-specific code

  • Configuration vs implementation

3. Scalability

  • Easy to add new features

  • Consistent organization patterns

  • Team-friendly structure


Root Level Structure


Detailed Folder Explanations

app/ - Next.js App Router

Purpose: Next.js 13+ App Router for routing and pages

Key Concepts:

  • Each folder represents a route

  • page.tsx = route component

  • layout.tsx = layout wrapper

  • [locale] = dynamic routing for i18n


components/ - Shared UI Components

Purpose: Reusable components used across the application

Component Categories:

  • UI Components: Basic, styleable primitives

  • Layout Components: Navigation and structure

  • Shared Components: Common functionality


features/ - Feature-Based Modules

Purpose: Self-contained business logic modules

Feature Structure:

  • components/ - Feature-specific UI components

  • hooks/ - Custom React hooks for logic

  • services/ - API calls and external integrations

  • types/ - TypeScript interfaces and types

  • utils/ - Feature-specific utility functions

  • index.ts - Clean exports for the feature


shared/ - Shared Utilities

Purpose: Code used across multiple features

Shared Categories:

  • Constants: Configuration values, API endpoints

  • Services: External API integrations, storage

  • Types: Shared TypeScript interfaces

  • Utils: Pure functions and helpers


context/ - React Context

Purpose: Global state management

Usage:

  • Global app state (user, auth, theme)

  • Avoid prop drilling

  • Shared state across components


i18n/ - Internationalization

Purpose: Multi-language support

Features:

  • Multi-language routing

  • Translation management

  • Locale-specific content


commons/ - Global Styles

Purpose: Application-wide styling

Contents:

  • Global CSS styles

  • CSS variables

  • Tailwind imports

  • Base styles and resets


middleware.ts - Next.js Middleware

Purpose: Request/response interception and API proxying

Key Features:

  • API Proxy to FastAPI Backend: Automatically routes /api/* requests to Python FastAPI

  • Internationalization: Handles locale routing (/en/, /fr/, /tl/)

  • Header Management: Forwards authentication headers (JWT tokens)

  • CORS Handling: Development-friendly CORS configuration

  • Error Handling: Graceful fallbacks for backend connection issues

Configuration:

How It Works:

Common Uses:

  • API request proxying to FastAPI backend

  • Authentication header forwarding

  • i18n routing and locale handling

  • CORS management for development

  • Static file optimization


API Integration Patterns

Frontend to FastAPI Communication

Service Layer Example

React Hook Integration


Import Patterns

Avoid These Patterns


Benefits of This Structure

Maintainability

  • Easy to find related code

  • Clear separation of concerns

  • Consistent organization

Scalability

  • Easy to add new features

  • No circular dependencies

  • Modular architecture

Team Collaboration

  • Different developers can work on different features

  • Clear ownership boundaries

  • Reduced merge conflicts

Performance

  • Better tree shaking

  • Lazy loading opportunities

  • Optimized bundle splitting


Best Practices

1. Feature Isolation

  • Keep feature code self-contained

  • Minimize cross-feature dependencies

  • Use shared utilities for common functionality

2. Clean Exports

  • Use index.ts files for clean exports

  • Avoid deep import paths

  • Group related exports

3. Consistent Naming

  • Use singular names for features

  • Follow consistent file naming

  • Use descriptive component names

4. Type Safety

  • Define types in feature-specific folders

  • Use shared types for common interfaces

  • Export types from index files


Configuration Files

Environment Variables (.env.local)

tailwind.config.ts

tsconfig.json


Additional Resources

Last updated

Was this helpful?