React Getting Started

Getting Started with React

There are several ways to start a React project. We’ll cover the two most popular modern approaches.

Vite is a next-generation build tool that provides blazing fast Hot Module Replacement (HMR) and near-instant server startup.

# Create a new React project with Vite
npm create vite@latest my-app -- --template react

# Navigate into the project
cd my-app

# Install dependencies
npm install

# Start the development server (http://localhost:5173)
npm run dev

Vite Project Structure

my-app/
โ”œโ”€โ”€ public/             โ† Static files (served as-is)
โ”‚   โ””โ”€โ”€ vite.svg
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ assets/         โ† Images, fonts
โ”‚   โ”œโ”€โ”€ App.jsx         โ† Root component
โ”‚   โ”œโ”€โ”€ App.css
โ”‚   โ”œโ”€โ”€ main.jsx        โ† Entry point
โ”‚   โ””โ”€โ”€ index.css
โ”œโ”€โ”€ index.html          โ† HTML shell
โ”œโ”€โ”€ vite.config.js      โ† Vite configuration
โ””โ”€โ”€ package.json

Option 2 โ€” Next.js (Full-Stack)

Next.js adds server-side rendering, file-based routing, and API routes on top of React. Choose it when you need SEO, server components, or a full-stack solution.

npx create-next-app@latest my-app
cd my-app
npm run dev

Essential npm Scripts

Command What it does
npm run dev Start development server with HMR
npm run build Create optimised production bundle in dist/
npm run preview Locally preview the production build
npm install <pkg> Add a dependency
npm install -D <pkg> Add a dev-only dependency
TipAlways use npm run dev during development โ€” never open index.html directly in the browser.

Your First Edit

Open src/App.jsx and replace everything with:

function App() {
  return (
    <div>
      <h1>Hello, React! โš›๏ธ</h1>
      <p>Edit src/App.jsx and save to see changes instantly.</p>
    </div>
  );
}

export default App;
WarningReact component names must start with a capital letter. <App /> works; <app /> is treated as an unknown HTML element.

๐Ÿง  Test Yourself

Which build tool is recommended for new React projects in 2024?