JavaScript and React course Β· Module 1: React Basics

React project structure

13 min read Β·
In this lesson8

A well-organized project structure is crucial for code maintainability, easy navigation, and scalability of React applications. In this lesson, we'll look at best practices for structuring React projects and understand how to organize files and components.

Basic React project structure

Whatever the tool, a larger React project (here a Vite one) usually has a similar structure:

my-react-project/
β”œβ”€β”€ node_modules/        # Installed dependencies
β”œβ”€β”€ public/              # Publicly accessible static files
β”œβ”€β”€ src/                 # Application source code
β”‚   β”œβ”€β”€ components/      # Reusable components
β”‚   β”œβ”€β”€ pages/           # Components representing entire pages
β”‚   β”œβ”€β”€ assets/          # Assets such as images, fonts, etc.
β”‚   β”œβ”€β”€ utils/           # Utility functions
β”‚   β”œβ”€β”€ services/        # Code responsible for API communication
β”‚   β”œβ”€β”€ hooks/           # Custom React hooks
β”‚   β”œβ”€β”€ context/         # React contexts
β”‚   β”œβ”€β”€ App.jsx          # Main application component
β”‚   └── main.jsx         # Application entry point
β”œβ”€β”€ index.html           # The HTML page the app is loaded into
β”œβ”€β”€ vite.config.js       # Vite configuration
β”œβ”€β”€ .gitignore           # Files ignored by Git
β”œβ”€β”€ package.json         # Project info and dependencies
└── README.md            # Project documentation

Let's discuss the most important directories and their purposes:

The src/ directory

The src/ directory is the heart of your React application. It contains all the logic, components, and assets. Here's how this directory is typically organized:

components/

Contains reusable components that are used in various parts of the application. There are several approaches to structuring components:

Flat structure

components/
β”œβ”€β”€ Button.jsx
β”œβ”€β”€ Button.css
β”œβ”€β”€ Header.jsx
β”œβ”€β”€ Header.css
β”œβ”€β”€ Footer.jsx
└── Footer.css

Grouped structure (preferred for larger projects)

components/
β”œβ”€β”€ Button/
β”‚   β”œβ”€β”€ Button.jsx
β”‚   β”œβ”€β”€ Button.css
β”‚   └── index.js
β”œβ”€β”€ Header/
β”‚   β”œβ”€β”€ Header.jsx
β”‚   β”œβ”€β”€ Header.css
β”‚   └── index.js
└── Footer/
    β”œβ”€β”€ Footer.jsx
    β”œβ”€β”€ Footer.css
    └── index.js

Using index.js files to export components simplifies imports:

// Instead of:
import Button from './components/Button/Button';

// You can write:
import Button from './components/Button';

pages/

Components representing entire pages or views in the application. In projects with routing, each file in this directory typically corresponds to a specific URL path:

pages/
β”œβ”€β”€ Home/
β”‚   β”œβ”€β”€ Home.jsx
β”‚   └── Home.css
β”œβ”€β”€ About/
β”‚   β”œβ”€β”€ About.jsx
β”‚   └── About.css
└── Dashboard/
    β”œβ”€β”€ Dashboard.jsx
    └── Dashboard.css

assets/

Contains all static assets used in the application:

assets/
β”œβ”€β”€ images/
β”‚   β”œβ”€β”€ logo.png
β”‚   └── background.jpg
β”œβ”€β”€ fonts/
β”‚   β”œβ”€β”€ roboto.ttf
β”‚   └── open-sans.ttf
└── styles/
    β”œβ”€β”€ variables.css
    └── global.css

utils/

Utility functions and tools used throughout the application:

utils/
β”œβ”€β”€ formatDate.js
β”œβ”€β”€ validation.js
└── helpers.js

services/

Code responsible for communicating with external APIs or services:

services/
β”œβ”€β”€ api.js           # Basic API configuration
β”œβ”€β”€ userService.js   # User-related queries
└── productService.js # Product-related queries

hooks/

Custom React hooks that can be used in multiple places in the application:

hooks/
β”œβ”€β”€ useLocalStorage.js
β”œβ”€β”€ useFetch.js
└── useForm.js

context/

React contexts used for managing global state:

context/
β”œβ”€β”€ AuthContext.js
β”œβ”€β”€ ThemeContext.js
└── CartContext.js

Organization by feature vs. by type

There are two main approaches to code organization:

Organization by type (shown above)

Files are grouped by their type (components, hooks, services, etc.).

Organization by feature

Files are grouped by feature or part of the application:

src/
β”œβ”€β”€ auth/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ hooks/
β”‚   β”œβ”€β”€ services/
β”‚   └── context/
β”œβ”€β”€ products/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ hooks/
β”‚   β”œβ”€β”€ services/
β”‚   └── utils/
└── checkout/
    β”œβ”€β”€ components/
    β”œβ”€β”€ hooks/
    └── services/

This structure is more scalable for large projects, especially when multiple people are working on them.

Naming conventions

Consistent naming is just as important as directory structure:

Components

  • Use PascalCase: Button.jsx, UserProfile.jsx
  • JSX files should have the .jsx extension (not required, but helps with identification)

Hooks

  • Always start with "use": useLocalStorage.js, useWindowSize.js

Contexts

  • Usually end with "Context": AuthContext.js, ThemeContext.js

Services and utilities

  • Use camelCase: apiService.js, formatDate.js

Styles in React

There are several approaches to organizing styles in React:

CSS Modules

Button/
β”œβ”€β”€ Button.jsx
└── Button.module.css

Styled Components

Button/
└── Button.jsx  # Contains styled-components definitions

SCSS/SASS

Button/
β”œβ”€β”€ Button.jsx
└── Button.scss

Folder structure best practices

In the React cosmic fleet, a well-organized folder structure is like an efficient logistics system on a spaceship - everything has its place and you can quickly find what you need. Here are proven practices for organizing React projects.

Feature-based vs Type-based organization

Two main approaches to organizing code in React projects:

Type-based organization

Files grouped by their technical role - all components together, all hooks together, etc.

src/
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ Button/
β”‚   β”œβ”€β”€ Card/
β”‚   β”œβ”€β”€ Modal/
β”‚   β”œβ”€β”€ UserProfile/
β”‚   β”œβ”€β”€ ProductCard/
β”‚   └── ShoppingCart/
β”œβ”€β”€ hooks/
β”‚   β”œβ”€β”€ useAuth.js
β”‚   β”œβ”€β”€ useProducts.js
β”‚   └── useCart.js
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ authService.js
β”‚   β”œβ”€β”€ productService.js
β”‚   └── cartService.js
└── utils/
    β”œβ”€β”€ validators.js
    └── formatters.js

Advantages:

  • Simpler for small and medium projects
  • Easy to understand for new developers
  • Good for SPA applications without distinct modules

Disadvantages:

  • In large projects, it's hard to find all files related to a single feature
  • Components from different features are mixed together
  • Harder to scale across teams

Feature-based organization

Files grouped by business feature - all files related to authentication together, all product-related files together, etc.

src/
β”œβ”€β”€ features/
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ LoginForm/
β”‚   β”‚   β”‚   β”œβ”€β”€ RegisterForm/
β”‚   β”‚   β”‚   └── PasswordReset/
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   β”œβ”€β”€ useAuth.js
β”‚   β”‚   β”‚   └── useSession.js
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── authService.js
β”‚   β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”‚   └── validators.js
β”‚   β”‚   └── index.js
β”‚   β”‚
β”‚   β”œβ”€β”€ products/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ ProductList/
β”‚   β”‚   β”‚   β”œβ”€β”€ ProductCard/
β”‚   β”‚   β”‚   └── ProductDetails/
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   β”œβ”€β”€ useProducts.js
β”‚   β”‚   β”‚   └── useProductFilters.js
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── productService.js
β”‚   β”‚   └── index.js
β”‚   β”‚
β”‚   └── cart/
β”‚       β”œβ”€β”€ components/
β”‚       β”‚   β”œβ”€β”€ ShoppingCart/
β”‚       β”‚   β”œβ”€β”€ CartItem/
β”‚       β”‚   └── Checkout/
β”‚       β”œβ”€β”€ hooks/
β”‚       β”‚   └── useCart.js
β”‚       β”œβ”€β”€ services/
β”‚       β”‚   └── cartService.js
β”‚       └── index.js
β”‚
β”œβ”€β”€ shared/
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ Button/
β”‚   β”‚   β”œβ”€β”€ Modal/
β”‚   β”‚   └── Card/
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”œβ”€β”€ useLocalStorage.js
β”‚   β”‚   └── useDebounce.js
β”‚   └── utils/
β”‚       β”œβ”€β”€ formatDate.js
β”‚       └── formatCurrency.js
β”‚
└── App.jsx

Advantages:

  • Easier to scale for large projects
  • All files related to a feature are in one place
  • Easier for team work (different teams can work on different features)
  • Easier to remove entire features
  • Better for code splitting

Disadvantages:

  • More complex for beginners
  • Requires more thought in design
  • Can lead to duplication for very small components

Naming conventions

Consistent naming is a key element of a well-organized project. In React, the following conventions apply:

Components - PascalCase

// GOOD - PascalCase
Button.jsx
UserProfile.jsx
ShoppingCart.jsx
ProductCard.jsx

// BAD - other conventions
button.jsx
user-profile.jsx
shopping_cart.jsx

Why PascalCase?

  • Distinguishes React components from regular functions
  • Convention adopted across the entire React ecosystem
  • JSX requires PascalCase for components (custom tags)

Hooks - camelCase with "use" prefix

// GOOD - camelCase with "use"
useAuth.js
useLocalStorage.js
useWindowSize.js
useProductFilters.js

// BAD - missing "use" or wrong convention
Auth.js
local-storage.js
WindowSize.js

Why "use" prefix?

  • React requires custom hooks to start with "use"
  • Allows React to validate hook rules
  • Immediately communicates that it's a hook

Contexts - PascalCase with "Context" suffix

// GOOD - PascalCase with "Context"
AuthContext.js
ThemeContext.js
CartContext.js
UserPreferencesContext.js

// BAD - missing "Context" or wrong convention
auth.js
theme-context.js
Cart.js

Utils and services - camelCase

// GOOD - camelCase
formatDate.js
validateEmail.js
apiService.js
authService.js

// BAD - other conventions
FormatDate.js
validate-email.js
API_Service.js

Where to keep different file types

Planning file locations in a project has enormous significance for its readability and scalability.

Hooks (custom hooks)

Feature-based approach:

features/
β”œβ”€β”€ auth/
β”‚   └── hooks/
β”‚       β”œβ”€β”€ useAuth.js
β”‚       └── useSession.js
└── products/
    └── hooks/
        └── useProducts.js

shared/
└── hooks/
    β”œβ”€β”€ useLocalStorage.js  # Shared hook
    β”œβ”€β”€ useDebounce.js      # Shared hook
    └── useFetch.js         # Shared hook

Type-based approach:

hooks/
β”œβ”€β”€ useAuth.js
β”œβ”€β”€ useSession.js
β”œβ”€β”€ useProducts.js
β”œβ”€β”€ useLocalStorage.js
β”œβ”€β”€ useDebounce.js
└── useFetch.js

Utils (utility functions)

Feature-based approach:

features/
β”œβ”€β”€ products/
β”‚   └── utils/
β”‚       β”œβ”€β”€ formatPrice.js
β”‚       └── filterProducts.js
└── auth/
    └── utils/
        └── validatePassword.js

shared/
└── utils/
    β”œβ”€β”€ formatDate.js       # Shared
    β”œβ”€β”€ debounce.js         # Shared
    └── cloneDeep.js        # Shared

Type-based approach:

utils/
β”œβ”€β”€ formatPrice.js
β”œβ”€β”€ formatDate.js
β”œβ”€β”€ validatePassword.js
β”œβ”€β”€ filterProducts.js
β”œβ”€β”€ debounce.js
└── cloneDeep.js

Types (TypeScript)

If you use TypeScript, you have several options for types:

Option 1: Next to the files that use them:

components/
└── Button/
    β”œβ”€β”€ Button.tsx
    β”œβ”€β”€ Button.types.ts     # Types only for Button
    β”œβ”€β”€ Button.styles.ts
    └── index.ts

Option 2: Centralized types:

types/
β”œβ”€β”€ user.types.ts
β”œβ”€β”€ product.types.ts
β”œβ”€β”€ cart.types.ts
└── api.types.ts

Option 3: Hybrid (best):

features/
└── products/
    β”œβ”€β”€ components/
    β”œβ”€β”€ types/              # Feature-specific types
    β”‚   β”œβ”€β”€ product.types.ts
    β”‚   └── filter.types.ts
    └── index.ts

shared/
└── types/                  # Shared types
    β”œβ”€β”€ common.types.ts
    └── api.types.ts

Example project structure - best practices

Combining feature-based for main features and type-based for shared resources:

my-react-app/
β”œβ”€β”€ public/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ features/                    # Main features
β”‚   β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ LoginForm/
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ LoginForm.tsx
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ LoginForm.styles.ts
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ LoginForm.test.tsx
β”‚   β”‚   β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚   β”‚   └── RegisterForm/
β”‚   β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ useAuth.ts
β”‚   β”‚   β”‚   β”‚   └── useSession.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   β”‚   └── authService.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ types/
β”‚   β”‚   β”‚   β”‚   └── auth.types.ts
β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ products/
β”‚   β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ProductList/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ProductCard/
β”‚   β”‚   β”‚   β”‚   └── ProductFilters/
β”‚   β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ useProducts.ts
β”‚   β”‚   β”‚   β”‚   └── useProductFilters.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   β”‚   └── productService.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ types/
β”‚   β”‚   β”‚   β”‚   └── product.types.ts
β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚
β”‚   β”‚   └── cart/
β”‚   β”‚       └── [similar structure]
β”‚   β”‚
β”‚   β”œβ”€β”€ shared/                      # Shared resources
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ Button/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Button.tsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Button.styles.ts
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Button.test.tsx
β”‚   β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ Modal/
β”‚   β”‚   β”‚   β”œβ”€β”€ Card/
β”‚   β”‚   β”‚   └── Layout/
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   β”œβ”€β”€ useLocalStorage.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ useDebounce.ts
β”‚   β”‚   β”‚   └── useFetch.ts
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”‚   β”œβ”€β”€ formatDate.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ validators.ts
β”‚   β”‚   β”‚   └── helpers.ts
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ types/
β”‚   β”‚   β”‚   β”œβ”€β”€ common.types.ts
β”‚   β”‚   β”‚   └── api.types.ts
β”‚   β”‚   β”‚
β”‚   β”‚   └── constants/
β”‚   β”‚       β”œβ”€β”€ routes.ts
β”‚   β”‚       └── config.ts
β”‚   β”‚
β”‚   β”œβ”€β”€ layouts/                     # Layout components
β”‚   β”‚   β”œβ”€β”€ MainLayout/
β”‚   β”‚   β”œβ”€β”€ AuthLayout/
β”‚   β”‚   └── DashboardLayout/
β”‚   β”‚
β”‚   β”œβ”€β”€ pages/                       # Page components (for routing)
β”‚   β”‚   β”œβ”€β”€ HomePage/
β”‚   β”‚   β”œβ”€β”€ ProductsPage/
β”‚   β”‚   β”œβ”€β”€ ProductDetailPage/
β”‚   β”‚   └── CartPage/
β”‚   β”‚
β”‚   β”œβ”€β”€ context/                     # Global contexts
β”‚   β”‚   β”œβ”€β”€ AuthContext.tsx
β”‚   β”‚   β”œβ”€β”€ ThemeContext.tsx
β”‚   β”‚   └── CartContext.tsx
β”‚   β”‚
β”‚   β”œβ”€β”€ router/                      # Routing configuration
β”‚   β”‚   β”œβ”€β”€ routes.tsx
β”‚   β”‚   └── ProtectedRoute.tsx
β”‚   β”‚
β”‚   β”œβ”€β”€ styles/                      # Global styles
β”‚   β”‚   β”œβ”€β”€ globals.css
β”‚   β”‚   β”œβ”€β”€ variables.css
β”‚   β”‚   └── reset.css
β”‚   β”‚
β”‚   β”œβ”€β”€ App.tsx
β”‚   └── index.tsx
β”‚
β”œβ”€β”€ .eslintrc.js
β”œβ”€β”€ .prettierrc
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ package.json
└── README.md

File organization rules

  1. Maximum 3 levels rule
    • Avoid nesting deeper than 3 levels
    • If you need more, your structure is probably too complex
// GOOD - 3 levels
src/features/products/components/

// BAD - too deep
src/modules/ecommerce/features/products/ui/components/
  1. Export through index.ts
    • Every folder with a component has an index.ts
    • Simplifies imports and hides implementation details
// components/Button/index.ts
export { default } from './Button';
export * from './Button.types';

// Usage
import Button from './components/Button';  // readable
// instead of
import Button from './components/Button/Button';  // verbose
  1. Colocation - keep together what is used together
    • Styles next to the component
    • Tests next to the component
    • Types next to the component
Button/
β”œβ”€β”€ Button.tsx          # Component
β”œβ”€β”€ Button.styles.ts    # Styles
β”œβ”€β”€ Button.test.tsx     # Tests
β”œβ”€β”€ Button.types.ts     # Types
└── index.ts            # Export
  1. Shared vs Feature-specific
    • If something is used in more than 2 places -> shared
    • If something is specific to a feature -> keep it in the feature

Practical tips for project structure

  1. Start simple - Don't over-engineer a complex structure at the beginning of a project. For a small application, type-based may suffice; only later refactor to feature-based as the project grows.

  2. Refactor when needed - Adapt the structure as the project develops. If a folder has more than 10-15 files, consider splitting it.

  3. Keep components small - Components should be small and focused on a single task. If a component has more than 200-300 lines, consider splitting it.

  4. Group related files - Keep files that are closely related together. If you always edit two files together, they should probably be in the same folder.

  5. Document - Add README.md to main directories to explain their purpose and structure.

  6. Consistency above all - Once you choose a layout (by type, by feature, or the hybrid described above), stick to it throughout the project. The worst thing is mixing styles at random.

Tools that help with code organization

  1. ESLint - Helps maintain consistent code style and catch potential errors
  2. Prettier - Automatically formats code according to established rules
  3. Import sorter - Sorts imports for better readability
  4. Path aliases - Simplifies imports using aliases like @components, @utils
// tsconfig.json or jsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@components/*": ["shared/components/*"],
      "@features/*": ["features/*"],
      "@utils/*": ["shared/utils/*"],
      "@hooks/*": ["shared/hooks/*"]
    }
  }
}

// Usage
import Button from '@components/Button';
import { useAuth } from '@features/auth';
import { formatDate } from '@utils/formatDate';

A well-organized project structure greatly facilitates work, especially in large teams and projects. Remember that there is no single "right" structure - the most important thing is that it is consistent, logical for your team, and scalable as the project grows.

Code for this lesson: App.jsx
import React, { useState } from 'react';

// Demonstration of Vite + React project structure
function ViteProjectDemo() {
  const [activeFile, setActiveFile] = useState('App.jsx');

  const fileStructure = [
    { name: 'node_modules/', type: 'folder', description: 'Installed packages' },
    { name: 'public/', type: 'folder', description: 'Static files' },
    { name: '  vite.svg', type: 'file', description: 'Vite logo' },
    { name: 'src/', type: 'folder', description: 'Source code' },
    { name: '  assets/', type: 'folder', description: 'Assets (images, fonts)' },
    { name: '  App.jsx', type: 'file', description: 'Main component', active: true },
    { name: '  App.css', type: 'file', description: 'Component styles' },
    { name: '  main.jsx', type: 'file', description: 'Entry point' },
    { name: '  index.css', type: 'file', description: 'Global styles' },
    { name: 'index.html', type: 'file', description: 'Main HTML (in the root directory!)' },
    { name: 'vite.config.js', type: 'file', description: 'Vite configuration' },
    { name: 'package.json', type: 'file', description: 'Project configuration' },
  ];

  const fileContents = {
    'App.jsx': `import { useState } from 'react'
import './App.css'

function App() {
  const [count, setCount] = useState(0)

  return (
    <>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => setCount(c => c + 1)}>
          count is {count}
        </button>
      </div>
    </>
  )
}

export default App`,
    'main.jsx': `import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'

ReactDOM.createRoot(
  document.getElementById('root')
).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)`,
    'vite.config.js': `import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
})`,
    'package.json': `{
  "name": "my-app",
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@vitejs/plugin-react": "^4.2.0",
    "vite": "^5.0.0"
  }
}`
  };

  return (
    <div style={{
      fontFamily: 'Arial, sans-serif',
      maxWidth: '1000px',
      margin: '0 auto',
      padding: '20px'
    }}>
      <header style={{
        background: 'linear-gradient(135deg, #61dafb 0%, #21a1f1 100%)',
        padding: '30px',
        borderRadius: '12px',
        color: 'white',
        textAlign: 'center',
        marginBottom: '30px'
      }}>
        <h1 style={{ margin: 0, fontSize: '2rem' }}>Vite + React</h1>
        <p style={{ margin: '10px 0 0 0', opacity: 0.9 }}>
          Project structure and basic files
        </p>
      </header>

      <div style={{
        backgroundColor: '#1e1e1e',
        borderRadius: '8px',
        padding: '20px',
        marginBottom: '30px',
        fontFamily: 'monospace'
      }}>
        <div style={{ color: '#888', marginBottom: '10px' }}>Terminal:</div>
        <div style={{ color: '#00ff00' }}>$ npm create vite@latest my-app -- --template react</div>
        <div style={{ color: '#888', marginTop: '5px' }}>Scaffolding project in /my-app...</div>
        <div style={{ color: '#00ff00', marginTop: '5px' }}>Done. Now run: cd my-app && npm install && npm run dev</div>
      </div>

      <div style={{ display: 'grid', gridTemplateColumns: '300px 1fr', gap: '20px' }}>
        <div style={{
          backgroundColor: '#252526',
          borderRadius: '8px',
          padding: '15px'
        }}>
          <h3 style={{ color: '#cccccc', margin: '0 0 15px 0', fontSize: '14px' }}>
            EXPLORER
          </h3>
          {fileStructure.map((item, index) => (
            <div
              key={index}
              onClick={() => item.type === 'file' && setActiveFile(item.name.trim())}
              style={{
                padding: '8px 10px',
                color: item.type === 'folder' ? '#dcb67a' : '#9cdcfe',
                cursor: item.type === 'file' ? 'pointer' : 'default',
                backgroundColor: activeFile === item.name.trim() ? '#094771' : 'transparent',
                borderRadius: '4px',
                fontSize: '14px',
                fontFamily: 'monospace'
              }}
            >
              {item.type === 'folder' ? 'β–Έ' : 'β€’'} {item.name}
            </div>
          ))}
        </div>

        <div style={{
          backgroundColor: '#1e1e1e',
          borderRadius: '8px',
          padding: '20px'
        }}>
          <div style={{
            color: '#888',
            fontSize: '12px',
            marginBottom: '10px',
            borderBottom: '1px solid #333',
            paddingBottom: '10px'
          }}>
            {activeFile}
          </div>
          <pre style={{
            color: '#d4d4d4',
            margin: 0,
            fontSize: '14px',
            lineHeight: '1.5',
            overflow: 'auto'
          }}>
            {fileContents[activeFile] || '// Select a file from the list'}
          </pre>
        </div>
      </div>

      <div style={{
        marginTop: '30px',
        padding: '20px',
        backgroundColor: '#fff3e0',
        borderRadius: '8px'
      }}>
        <h3 style={{ color: '#e65100', marginTop: 0 }}>Startup commands</h3>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: '15px' }}>
          <div style={{ backgroundColor: 'white', padding: '15px', borderRadius: '8px' }}>
            <code style={{ color: '#e65100' }}>npm run dev</code>
            <p style={{ margin: '5px 0 0 0', fontSize: '14px', color: '#666' }}>
              Starts the development server
            </p>
          </div>
          <div style={{ backgroundColor: 'white', padding: '15px', borderRadius: '8px' }}>
            <code style={{ color: '#e65100' }}>npm run build</code>
            <p style={{ margin: '5px 0 0 0', fontSize: '14px', color: '#666' }}>
              Builds the production version
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}

export default function App() {
  return <ViteProjectDemo />;
}

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What is the main purpose of the src/ folder in a React project?

  2. 2. In which folder are the main React source files located?

  3. 3. Where is the index.html file located in a React project created with Vite?

Hands-on tasks in the game

  • Vertical ordering

    Arrange the file hierarchy of a React application:

  • Click in order

    Arrange the React development workflow:

  • Code editor

    In App.jsx, create the basic structure of a component: a React import, a functional App component that returns JSX, and a default export (export default).

  1. QuestionThe src folder in a React project
  2. QuestionReact project structure
  3. QuestionThe index.html file in a Vite project
  4. Put in orderProcess: Application structure
  5. Click in orderSorting: Development process
  6. Coding taskPractice: Component file structure

Useful articles