Chapter 2 of 20 · React for Beginners — Complete Free Tutorial
Setting Up React with Vite
The React for Beginners series uses Vite for every example, because a fast, boring toolchain lets you focus on React itself. This chapter gets a project running on your machine: what to install, how to scaffold an app, what each generated file does, and how the development and production workflows differ.
Why Vite instead of the old setup
For years, Create React App was the default: a large dependency tree that bundled your whole app before every refresh. Vite (French for "fast") flipped the strategy. In development it serves your modules as native ES modules, so the browser loads only what the page needs and updates appear in milliseconds. For production it bundles and minifies with Rollup, producing small, cacheable files. Create React App is now effectively in maintenance mode, so new projects start with Vite — or with Next.js and other frameworks once you outgrow a single-page app.
| Task | Create React App (history) | Vite (this series) |
|---|---|---|
| Dev server start | Seconds to minutes, full bundle first | Almost instant, native modules |
| Refresh on save | Re-bundles the app | Hot module replacement, only the changed file |
| Production build | Webpack, slow on large apps | Rollup, parallel and quick |
What you need installed
You need Node.js (the current LTS version is ideal) and its bundled npm. A terminal check confirms both:
node --version
npm --version
Expected output:
v22.11.0
10.9.0
Any recent major version works. If the commands are not found, install the LTS build from nodejs.org first and reopen your terminal.
Scaffold the project
One npm command creates the folder, installs React and wires up the scripts. The -- --template react flag asks for plain JavaScript JSX; a react-ts template exists for TypeScript, which this series does not require.
npm create vite@latest my-first-app -- --template react
cd my-first-app
npm install
npm run dev
Expected output:
VITE v6.x.x ready in 400 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
Open the local URL and you should see the Vite and React logos with a counter already working. Every time you save a file the browser updates without a manual refresh — that is hot module replacement.
Tour of the generated files
The template is deliberately small. These are the files you will touch in almost every session:
| File | Role |
|---|---|
index.html | The single real page; it loads src/main.jsx as a module |
src/main.jsx | Entry point: attaches React to a DOM node |
src/App.jsx | The root component; the first file you will edit |
src/App.css, src/index.css | Styles for the demo; delete or replace freely |
vite.config.js | Build configuration and plugins |
package.json | Scripts (dev, build, preview) and dependencies |
The whole mounting ritual lives in main.jsx — you will never rewrite it, but you should recognise it:
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>,
);
Expected output: nothing visible by itself — it finds the <div id="root"> declared in index.html and renders App inside it. StrictMode is a development aid that highlights risky patterns such as effects without cleanup; it changes no production behaviour.
Develop, build and preview
Two commands cover the two halves of your workflow:
npm run dev # development: instant updates, helpful errors
npm run build # production: outputs an optimised dist/ folder
npm run preview # serves dist/ locally so you can check the real build
Expected output: after npm run build, the terminal reports the bundle sizes and a dist/ folder appears containing index.html plus hashed files in assets/. Those static files are exactly what you deploy in chapter 20.
How this series uses the project
Every later chapter assumes this layout. Keep one practice app open while you read: create src/components/ and src/examples/ folders now, type each chapter's snippets into a fresh file, and render them from App.jsx. Typed examples stick far better than copied ones.
Common mistakes
- Running
npm run devin the wrong folder — the command must run wherepackage.jsonlives, inside the project directory. - Editing
dist/orindex.htmlinstead ofsrc/— the build output is regenerated on every production build; your components live insrc/. - Forgetting
npm install— scaffolding createspackage.json, but dependencies download separately; missingnode_modulesmeans "command not found" errors. - Following Create React App tutorials —
npm startandpublic/index.htmldo not apply to a Vite project; usenpm run devand the rootindex.html. - Editing files while the terminal shows a syntax error — read Vite's error overlay first; it names the file and line that broke.
Practice checklist
- Scaffold a project with the exact commands above and confirm the counter demo renders on
localhost:5173. - Open
src/App.jsx, change the heading text, save, and watch the browser update without a refresh. - Delete the demo CSS imports and logos; make the page render a plain
<h1>you wrote yourself. - Run
npm run buildandnpm run preview, then confirm your edited page appears from the production bundle. - Break a file on purpose (remove a bracket), read the error overlay, and fix it.
The toolchain is running. Time to write some React — Your First Component.
Frequently Asked Questions
React is a JavaScript library for building user interfaces from reusable components. It manages how your UI updates when data changes, so you describe what the screen should look like and React performs the efficient DOM updates. It is one of the most in-demand frontend skills and powers products from Netflix to Instagram, which makes it an excellent next step after core JavaScript.
You should be comfortable with functions, arrays and objects (map, filter, spread), destructuring, modules and basic async/await. React itself is written in plain JavaScript — JSX is a syntax layer on top. If any of those feel shaky, revisit the JavaScript fundamentals first and this series will go much faster.
React is a library focused only on the UI layer — you choose your own router, data layer and tooling. Angular is a full framework that ships routing, forms, HTTP client and testing conventions as one opinionated package. React's flexibility and huge ecosystem make it the common choice for teams that want to compose their own stack.
A component is a reusable function that returns UI. It receives input through props, keeps private data in state, and can render other components. Pages are composed by nesting small components (navbar, card list, footer) instead of one giant file, which keeps code testable and easy to change.
JSX is a syntax extension that looks like HTML but compiles to React's create() calls — it is a description of UI, not markup the browser parses. Small differences matter: class becomes className, style takes an object, and you embed JavaScript expressions inside curly braces. Once compiled, React turns that description into real DOM nodes.
Props are inputs passed from a parent — they are read-only inside the child, like function arguments. State is private, mutable data owned by a component; updating it triggers a re-render. The rule of thumb: values that come from outside the component are props; values that change over the component's lifetime are state.
Rendering means calling your component function to produce a description of the UI for the current state and props. It does not always mean DOM work — React compares the new description with the previous one (reconciliation) and applies only the differences to the real DOM. This is why updating state 100 times does not mean 100 full page rebuilds.
An effect runs after the component has committed to the screen — after render, not during it. You list dependencies in an array, and React re-runs the effect only when one of them changes. If the effect returns a cleanup function, React runs it before the next re-run and on unmount, which is where you cancel timers, abort fetches and remove listeners.
Use Vite. Create React App is effectively in maintenance mode, while Vite starts instantly (native ES modules) and builds fast, and it is the default choice for new React projects. This series uses Vite for every example — you only need Node.js installed to follow along.
React apps compile to static files (HTML, CSS and JavaScript), so any static host works: Netlify, Vercel, GitHub Pages or Cloudflare Pages. Run the production build, upload the dist folder, and configure a single-page-application fallback so unknown paths load index.html — routing chapters explain why that fallback is required.