Chapter 1
Getting Started
What React is, why people use it, and how a React project is put together.
- What problem React solves
- How to create a project with Vite
- What each file in a new project does
- Useful tools: React DevTools, Prettier, ESLint
What is React?
React is a JavaScript library for building user interfaces (the buttons, lists and forms you see on a web page) out of small, reusable pieces called components.
Think of a web page as a Lego model. Without React, you build the whole model by hand, and every time something changes you find the right brick and swap it yourself. With React, you describe what the model should look like right now, and React works out which bricks to change.
The old way vs the React way
Say you want a counter that goes up when a button is clicked. In plain JavaScript you tell the browser every step (this is called imperative code):
In React you describe what the screen should show for a given value, and React updates the page for you (this is called declarative code):
For one counter the difference is small. For an app with hundreds of moving parts, the React way stays much easier to reason about. The core idea is:
What you need first
- Basic HTML, CSS and JavaScript. You especially need functions, arrays, objects, arrow functions (
() => {}), andimport/export. - Node.js installed. Check it in a terminal with
node -v. Node runs the development tools; your React code itself runs in the browser. - A code editor. VS Code is the most popular choice.
Creating a project
The official docs recommend two kinds of setup:
| Option | Good for | Examples |
|---|---|---|
| A framework | Real, full apps with routing (multiple pages), data loading and server rendering | Next.js, React Router |
| A build tool | Learning, small apps, or building from scratch | Vite (what your sample-test-react uses) |
To learn React itself, Vite is perfect: it's small, fast and has no extra concepts. Create a project like this:
Open the URL it prints (usually http://localhost:5173). Now edit src/App.jsx and save: the browser updates instantly, without reloading the page. This is called HMR (Hot Module Replacement).
What's inside a new project
How the page actually starts
Step 1. The browser loads index.html, which contains one empty box and a script tag:
Step 2. main.jsx runs. It tells React to take over that box and draw <App /> inside it:
Step 3. React calls your App function, takes the UI it returns, and puts it into the real page.
react knows about components, state and hooks. react-dom knows how to put them into a web page (the DOM). React Native uses the same react package to draw on phones instead.
StrictMode?
A development-only helper. It shows nothing on the page. It deliberately runs some of your code twice to catch mistakes early. If a console.log prints twice in development, this is why. It does not happen in the production build.
Useful scripts
| Command | What it does |
|---|---|
npm run dev | Starts the dev server with instant updates. Use it while coding. |
npm run build | Creates an optimised production version in dist/. It also shows errors that the dev server might hide. |
npm run preview | Serves the built dist/ folder locally so you can check it. |
npm run lint | Checks your code for common mistakes. |
Tools worth installing
- React Developer Tools (a browser extension for Chrome, Firefox or Edge). It adds a "Components" tab where you can see your component tree and each component's props and state live.
- Prettier (a VS Code extension). It formats your code automatically. Turn on "Format on Save".
- ESLint (or oxlint, which your project uses). It warns about bugs such as breaking the Rules of Hooks.
Common beginner mistakes
- Running
npm run devin the wrong folder โ "Missing script: dev". Runcdinto the folder that containspackage.jsonfirst. - Forgetting
npm installafter cloning or creating a project โ "vite: command not found". - Opening
index.htmlby double-clicking โ a blank page. A React project must be opened through the dev server URL. - Editing files in
node_modules. Your changes are lost on the next install. Only editsrc/.
More examples
Example 1: adding a new component, end to end
This is the everyday workflow: create a file, export a component, import it, use it.
Example 2: importing an image
Put a file in src/assets/ and import it. Vite gives you the image's URL:
Files in public/ are not imported. You refer to them by absolute path: <img src="/vite.svg" />.
Example 3: reading the error overlay
If you make a syntax mistake, Vite shows a red overlay in the browser:
Read it in this order: file (App.jsx) โ line:column (8:10) โ message ("Unterminated JSX contents" = a tag isn't closed). Here <h1> is missing </h1>.
Example 4: console.log is your friend
You'll see the log twice in development, because of StrictMode. Logging is the easiest way to see when your components run.
Recap
- React builds UIs out of components, and you describe what to show, not how to change the page.
- Create a learning project with
npm create vite@latest. For full apps, the docs recommend a framework. index.htmlโmain.jsxโApp.jsxis how every Vite React app starts.react= components and hooks;react-dom= putting them on a web page.
Exercises
1. Change the first screen
In your project, replace everything inside App's return (...) with a single heading that says "Hello, React!". Save and watch the browser update.
Show solution
2. Find the root
Open the browser DevTools (F12) โ Elements tab. Find <div id="root">. What's inside it? Now open the Components tab (React DevTools) and compare.
Show answer
The Elements tab shows the real HTML React produced (<h1>, <div>โฆ). The Components tab shows your components (App, and any others). React turns the second into the first.
3. A new component file ๐ข easy
Create src/components/Footer.jsx that returns <footer>Made by <your name></footer>, and render it at the bottom of App.
Show solution
4. Show an imported image ๐ข easy
Import src/assets/react.svg and show it at 120px wide under your heading.
Show solution
5. Global styles ๐ข easy
Make the whole page use a light grey background and the Georgia font. Which CSS file should you change?
Show solution
src/index.css is imported in main.jsx, so it applies to the whole page:
6. Change the browser tab title ๐ข easy
The tab says "Vite + React" (or similar). Change it to "My React Playground".
Show solution
Edit the <title> tag in index.html at the project root: <title>My React Playground</title>.
7. Break it on purpose ๐ก medium
Make each of these mistakes one at a time, save, and read the error. Then undo it.
- Delete a closing
</h1>. - Rename
Apptoappinmain.jsxonly. - Import a file that doesn't exist:
import X from './Nope'.
Show what you should see
- "Unterminated JSX contents" or "Expected corresponding JSX closing tag".
- "app is not defined", or a blank page with a console error.
- "Failed to resolve import './Nope'".
Learning to recognise these messages will save you hours.
8. Build for production ๐ก medium
Run npm run build, then look inside the dist/ folder. What files are there? Then run npm run preview and open the URL.
Show answer
dist/index.html plus dist/assets/ with one bundled .js file, one .css file and your images, each with a hash in its name (like index-CENbLOPi.js). This is everything a web server needs. Vite has combined and minified all your files.
9. Explore with React DevTools ๐ก medium
Install React Developer Tools. In your sample-test-react app, find the Profile component in the Components tab. What props does it have? Can you edit a prop in DevTools and see the page change?
Show answer
Select Profile โ the right panel shows props: { name: "..." }. Double-click the value to edit it. The page updates immediately, which is great for experimenting.
10. A fresh playground project ๐ด hard
Without looking at this chapter, create a brand-new Vite React project called playground, clean out the starter code, and render a page with your name, a photo and a short bio.
Show solution
๐ Official docs: Quick Start ยท Installation ยท Creating a React App ยท React Developer Tools