Getting Started with React · Lesson 2 of 31
Create a React App with Vite
React code needs a build step: JSX has to be turned into JavaScript, files bundled and the browser reloaded while you work. Vite does all of this with almost no configuration and starts in well under a second. It is the standard way to start a client-side React app – the old create-react-app tool is deprecated and should not be used for new projects.
In this lesson
- Installing Node.js and creating a project
- The folder structure Vite creates
- What
main.jsx,createRootandStrictModedo - Dev server, production build and preview
Step 1: Install Node.js
Install the current LTS release of Node.js from nodejs.org (it includes npm). Check it in a terminal:
node --version # v22 or newer
npm --version
Step 2: Create the project
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
Open the URL it prints (usually http://localhost:5173). Use --template react-ts instead if you want TypeScript from day one – the React + TypeScript lesson later in this course covers it.
What each command does
- npm create vitecopies a starter project into my-react-app
- npm installdownloads react, react-dom and vite into node_modules
- npm run devstarts the dev server with hot module replacement
The folder structure
my-react-app
- index.html – the single HTML page; has
<div id="root"> - package.json – dependencies and scripts
- vite.config.js – Vite configuration (the React plugin)
- public/ – files copied as-is (favicon, robots.txt)
- src/
- main.jsx – entry point: mounts React into #root
- App.jsx – the root component
- App.css, index.css – styles
- assets/ – images imported from code
main.jsx: where React starts
// src/main.jsx
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App.jsx";
createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>
);
createRootconnects React to one DOM element. React controls everything inside it.render(<App />)draws the root component; every other component is rendered from there.StrictModeis a development-only helper. It renders components twice and re-runs effects once to expose bugs such as impure components or missing cleanup. It does nothing in production builds.
App.jsx: your first component
Replace the starter App.jsx with this:
// src/App.jsx
import { useState } from "react";
export default function App() {
const [name, setName] = useState("");
return (
<main>
<h1>Hello, {name || "React"}!</h1>
<input placeholder="Your name" value={name} onChange={(e) => setName(e.target.value)} />
</main>
);
}
Hello, React! [Your name…] › type “Asha” into Your name Hello, Asha! [Asha]
Save the file and the browser updates instantly, keeping the component's state – that's hot module replacement.
Scripts
| Command | What it does |
|---|---|
npm run dev | Development server with instant updates |
npm run build | Optimised production files in dist/ (minified, hashed file names) |
npm run preview | Serves dist/ locally to test the production build |
npm run lint | ESLint, including the rules of hooks |
Deploying
The dist/ folder is static files – upload it to any static host (Netlify, Vercel, Cloudflare Pages, S3, or your own server). For a single-page app with client-side routing, configure the host to serve index.html for unknown paths.
Environment variables
Only variables prefixed with VITE_ are exposed to your code, as import.meta.env.VITE_API_URL. Everything in a client bundle is public, so never put secret keys there.
Practice
Try it yourself
Create a project, then change
App.jsxto show today's course and a button that hides or shows it.Show solution
import { useState } from "react"; export default function App() { const [visible, setVisible] = useState(true); return ( <main> <button onClick={() => setVisible(!visible)}>{visible ? "Hide" : "Show"}</button> {visible && <p>Today: React with Vite</p>} </main> ); }[Hide] Today: React with Vite › click “Hide” [Show] › click “Show” [Hide] Today: React with Vite
Quick check
Which file mounts React into the page?
src/main.jsx, using createRoot(...).render(...).
Does StrictMode's double rendering happen in production?
No, only in development.
How do you expose an environment variable to client code in Vite?
Prefix it with VITE_ and read import.meta.env.VITE_NAME.
Summary
npm create vite@latest app -- --template reactcreates a React project.- main.jsx mounts <App /> with createRoot; StrictMode checks for bugs in development.
- dev for development, build for production files in dist/, preview to test them.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.