Project Setup in React
Starting a new React project involves choosing the right build tool and framework. The ecosystem has evolved significantly, shifting away from older tools like Create React App toward faster, more feature-rich alternatives.
Choosing a Toolchain
1. Vite (Recommended for SPAs)
Vite is a modern build tool that provides a significantly faster and leaner development experience for modern web projects. It uses native ES modules in the browser, making server start and HMR (Hot Module Replacement) nearly instantaneous.
To create a new Vite project:
npm create vite@latest my-react-app -- --template react
# or with TypeScript
npm create vite@latest my-react-app -- --template react-ts
2. Next.js (Recommended for Full-Stack / SSR)
Next.js is a React framework that provides building blocks to create full web applications. It supports Server-Side Rendering (SSR), Static Site Generation (SSG), and React Server Components out of the box.
To create a new Next.js project:
npx create-next-app@latest my-next-app
3. Create React App (Deprecated)
Create React App (CRA) was the standard for many years. However, it is no longer recommended by the official React documentation due to slow build times and lack of modern features. If you are starting a new project, use Vite or a framework like Next.js/Remix.
Package Managers: npm vs. yarn vs. pnpm
Choosing a package manager can affect your project's installation speed and disk space.
- npm: The default package manager for Node.js. It's universally understood and works well.
- yarn: Created by Facebook to solve some of early npm's shortcomings (like speed and lockfiles).
- pnpm: Fast, disk space efficient package manager. It uses a global store and hard links to save space, meaning you don't have multiple copies of the same dependency across different projects.
Recommendation: pnpm is highly recommended for modern projects due to its speed and efficiency.
# Installing pnpm
npm install -g pnpm
# Creating a project with pnpm
pnpm create vite my-app --template react
Standard Project Structure
A well-organized project structure makes scaling and maintaining your application much easier. A common structure for a React SPA (like Vite) looks like this:
my-react-app/
├── public/ # Static assets that bypass the bundler (e.g., favicon)
│ └── vite.svg
├── src/
│ ├── assets/ # Images, fonts, etc. handled by the bundler
│ ├── components/ # Reusable UI components (Buttons, Cards)
│ ├── features/ # Feature-based domains (e.g., auth, dashboard)
│ ├── hooks/ # Custom React hooks (e.g., useAuth, useFetch)
│ ├── pages/ # Route-level components
│ ├── services/ # API calls and external integrations
│ ├── store/ # Global state management (Redux, Zustand)
│ ├── utils/ # Helper functions and constants
│ ├── App.jsx # Root component
│ └── main.jsx # Entry point (ReactDOM.createRoot)
├── .gitignore
├── index.html # Main HTML file
├── package.json # Project metadata and dependencies
└── vite.config.js # Vite configuration
Feature-Driven Structure
As apps grow, organizing files purely by type (all components in one folder, all hooks in another) becomes unmanageable. A Feature-Driven Architecture groups files by their domain.
src/
└── features/
└── authentication/
├── components/
│ ├── LoginForm.jsx
│ └── SignupForm.jsx
├── hooks/
│ └── useAuth.js
└── api/
└── authApi.js
Setting up your project correctly from day one with modern tools like Vite or Next.js, using an efficient package manager like pnpm, and establishing a scalable folder structure sets the foundation for a successful codebase.