Chapter 1

Getting Started

What React is, why people use it, and how a React project is put together.

You will learn
  • 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:

UI = f(data)The screen is the output of a function of your data. Change the data, and React redraws the screen to match.

What you need first

Creating a project

The official docs recommend two kinds of setup:

OptionGood forExamples
A frameworkReal, full apps with routing (multiple pages), data loading and server renderingNext.js, React Router
A build toolLearning, small apps, or building from scratchVite (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.

Two packages, two jobs 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.
What is 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

CommandWhat it does
npm run devStarts the dev server with instant updates. Use it while coding.
npm run buildCreates an optimised production version in dist/. It also shows errors that the dev server might hide.
npm run previewServes the built dist/ folder locally so you can check it.
npm run lintChecks your code for common mistakes.

Tools worth installing

A word about the React Compiler The docs also describe the React Compiler, a build-time tool that automatically optimises your components. You don't need it to learn React. Write normal, clean React code, and the compiler can be added later.

Common beginner mistakes

  • Running npm run dev in the wrong folder โ†’ "Missing script: dev". Run cd into the folder that contains package.json first.
  • Forgetting npm install after cloning or creating a project โ†’ "vite: command not found".
  • Opening index.html by 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 edit src/.

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.jsx is 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.

  1. Delete a closing </h1>.
  2. Rename App to app in main.jsx only.
  3. Import a file that doesn't exist: import X from './Nope'.
Show what you should see
  1. "Unterminated JSX contents" or "Expected corresponding JSX closing tag".
  2. "app is not defined", or a blank page with a console error.
  3. "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