Skip to main content

React folder structure

 Your proposed folder structure—separating components and pages—is a very common and practical starting point for React applications, especially when using routing libraries like React Router.


Here is how to think about this structure, along with a few minor corrections and improvements.

Clarification on Your Structure

  1. src/pages: Contains the main page views of your app (e.g., Home.jsx, About.jsx, Dashboard.jsx). Each page represents a distinct route/URL in your application.

  2. src/components: Contains reusable UI elements (e.g., Button.jsx, Navbar.jsx, Card.jsx) that get imported into your pages or used across multiple areas.

How to Think About Structuring React Apps

Instead of placing all components into one massive components folder as your app grows, it helps to organize components by reusability and scope:

  • Shared / Generic Components (src/components/common or src/components/ui):
    UI building blocks like buttons, modals, input fields, and headers that don't depend on specific business logic.

  • Page-Specific Components (src/pages/HomePage/components):
    Components used only on a specific page. Keeping them close to the page file makes it easier to delete or update that feature without affecting other parts of the app.

  • Feature-Based Folders (Alternative approach for larger apps):
    Grouping files by feature (e.g., src/features/auth, src/features/dashboard) rather than by type (components, pages, state).

Recommended Folder Layout

For a clean and scalable setup, here is a standard layout built on your idea:

Plaintext
src/
├── assets/          # Images, icons, global styles
├── components/      # Reusable UI components used across pages
│   ├── Button.jsx
│   ├── Navbar.jsx
│   └── Modal.jsx
├── pages/           # Route views
│   ├── Home.jsx
│   ├── About.jsx
│   └── Dashboard.jsx
├── services/        # API calls / HTTP requests
├── hooks/           # Custom React hooks
└── App.jsx          # Main App component & router setup
Would you like advice on setting up routing with React Router, or managing state within this structure?


Your proposed folder structure—separating components and pages—is a very common and practical starting point for React applications, especially when using routing libraries like React Router.

Here is how to think about this structure, along with a few minor corrections and improvements.

Clarification on Your Structure

  1. src/pages: Contains the main page views of your app (e.g., Home.jsx, About.jsx, Dashboard.jsx). Each page represents a distinct route/URL in your application.

  2. src/components: Contains reusable UI elements (e.g., Button.jsx, Navbar.jsx, Card.jsx) that get imported into your pages or used across multiple areas.

How to Think About Structuring React Apps

Instead of placing all components into one massive components folder as your app grows, it helps to organize components by reusability and scope:

  • Shared / Generic Components (src/components/common or src/components/ui):
    UI building blocks like buttons, modals, input fields, and headers that don't depend on specific business logic.

  • Page-Specific Components (src/pages/HomePage/components):
    Components used only on a specific page. Keeping them close to the page file makes it easier to delete or update that feature without affecting other parts of the app.

  • Feature-Based Folders (Alternative approach for larger apps):
    Grouping files by feature (e.g., src/features/auth, src/features/dashboard) rather than by type (components, pages, state).

Recommended Folder Layout

For a clean and scalable setup, here is a standard layout built on your idea:

Plaintext
src/
├── assets/          # Images, icons, global styles
├── components/      # Reusable UI components used across pages
│   ├── Button.jsx
│   ├── Navbar.jsx
│   └── Modal.jsx
├── pages/           # Route views
│   ├── Home.jsx
│   ├── About.jsx
│   └── Dashboard.jsx
├── services/        # API calls / HTTP requests
├── hooks/           # Custom React hooks
└── App.jsx          # Main App component & router setup
Would you like advice on setting up routing with React Router, or managing state within this structure?

Comments