In the last lesson you created first-app, ran it and changed its heading.
This lesson explains what happened in between: how a request for
http://localhost:5173/ turns into React's output on the page, which files take
part, and which ones you will edit. You need first-app from
Set up Node.js and run your first React app.
The Files in first-app
Open the first-app folder in your editor. These are the files the template
created and the ones npm install added:
| File or folder | What it is | Do you edit it? |
|---|---|---|
index.html | The page the browser loads first; it contains an empty element for React to fill | Rarely: the page title, perhaps |
src/main.jsx | The entry point: starts React and tells it where to render | Rarely |
src/App.jsx | The top-level component: what the page shows | Constantly |
src/index.css, src/App.css | The template's styles | When styling |
src/assets/ | Images imported by components | When adding images |
public/ | Files served as they are, at the site's root address (for example /favicon.svg) | Occasionally |
package.json | The project's name, commands (scripts) and the packages it depends on | When adding packages or commands |
package-lock.json | The exact versions npm installed | Never by hand; npm maintains it |
node_modules/ | The installed packages | Never; recreate with npm install |
vite.config.js | Vite's settings for this project | Rarely |
.oxlintrc.json | Settings for the template's code checker | Replaced later in the course |
README.md, .gitignore | Notes from the template, and a list of files Git should ignore | No |
Files starting with a dot are hidden by default in macOS Finder and some editors. That does not matter here.
Most of your work will happen in src/. The rest of this lesson follows the
four files that connect the browser to your component.
From Request to Page

How the browser gets from the address Vite printed to your component's output. Solid arrows show what loads or fills what; the dashed arrow is an import.
Follow the numbers:
- The browser asks the dev server for
http://localhost:5173/. - The server answers with
index.html. The browser reads it, creates an empty<div id="root">, and finds a script it must load:/src/main.jsx. - The browser asks for
/src/main.jsx. That file is JSX, which browsers do not understand, so Vite converts it to plain JavaScript before sending it. The code importsApp(step 4) and tells React to render it into the root element. src/App.jsxdefines theAppcomponent, a function that returns what the page should show.- React runs
Appand puts what it returns inside<div id="root">. That is what you see.
The arrow from index.html down to the page is the empty element; the arrow
from main.jsx is React filling it. Everything React will ever show lives
inside that one element.
index.html: the Shell
<!-- index.html (template) -->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>first-app</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
The first line is a comment naming the file; it is not in the real file.
Two lines matter:
<div id="root"></div>is empty on purpose. React fills it. Theidis howmain.jsxfinds it.<script type="module" src="/src/main.jsx">loads the entry point.type="module"lets that file useimportandexport. The path starts with/, meaning "from the project's root folder" as the dev server sees it.
href="/favicon.svg" comes from the public/ folder, which the dev server
serves at the site's root. In Vite projects index.html sits at the top of the
project rather than in public/, because it is the starting point Vite reads.
src/main.jsx: Starting React
// src/main.jsx (template)
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>,
)
Read it in two parts.
The imports. StrictMode comes from the react package, and createRoot
from react-dom/client. React is split in two: react holds what components
are written with, and react-dom connects them to a browser page. The CSS
import tells Vite to add those styles to the page. App is the default export
of ./App.jsx; the ./ means "in the same folder as this file".
The call. Read it from the inside out:
document.getElementById('root')is the standard browser function that finds the empty<div id="root">fromindex.html.createRoot(...)creates a React root for that element: the place React manages from now on..render(...)tells React what to display there. The argument is JSX, the HTML-like syntax that the next lessons teach:<App />means "theAppcomponent".
<StrictMode> wraps the app in extra checks that run only in development.
They make some mistakes visible early. For example, React renders components
twice and runs some setup code twice so that code which is not repeatable
stands out. You will see this when it matters, in
How React renders. It has
no effect on a production build. Leave it in.
This file rarely changes. Every lesson in the course keeps the same pattern:
one root, filled by App.
src/App.jsx: Where Your Work Goes
App.jsx exports a function named App that returns JSX describing the page.
The template's version contains images, a heading, a counter button and
links. You changed its heading in the last lesson. A function like this, whose
name starts with a capital letter and which returns JSX, is a component.
The next lesson builds components from scratch, so this one only needs you to
know where App comes from and where its output goes.
package.json: Scripts and Dependencies
These are the parts of first-app/package.json you will use:
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "oxlint",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.3.1",
"react-dom": "^19.3.1"
},
scriptsare named commands.npm run devrunsvite, the dev server.npm run buildmakes a production version, andnpm run previewserves that version locally; both come at the end of the course.npm run lintruns the template's code checker, which a later lesson replaces.dependenciesare packages the app needs when it runs in the browser: React.devDependencies, further down the file, are tools used only while developing, such as Vite.^19.3.1means "19.3.1 or any later 19.x version". A freshnpm installin a few months could therefore install a newer React. Thepackage-lock.jsonfile records exactly what was installed, so the same versions come back. The course's later projects list exact versions without^, so that what you install matches what the lessons were checked with.
vite.config.js: the React Plugin
// vite.config.js (template)
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
})
Vite works with many frameworks. The React plugin adds what React needs:
converting JSX in .jsx files and updating components in place when you
save. Without it, Vite would not understand the JSX in main.jsx. You will
not change this file in the course.
Exercise: Break the Connection on Purpose
The best way to see how these files depend on each other is to break the link between them and read the result.
Starting state: first-app from the previous lesson. In a terminal in
react-foundations-practice/first-app, start the dev server with
npm run dev and open the address it prints, with the browser's developer
console open (F12, or Cmd+Option+I on macOS).
- In
index.html, change<div id="root"></div>to<div id="app"></div>. Do not changemain.jsx. Save. - Look at the page and the console.
What happens: the page goes blank, and the console shows a red error whose message is:
Target container is not a DOM element.
The console usually also lists where the error came from; look for main.jsx
in that list. document.getElementById('root') found nothing, because no element
has that id any more, so it returned null. createRoot needs a real element
(a DOM element) and refuses null. The message names the problem precisely
once you know that "container" is the element you passed in.
- Change the
idback torootand save. The page returns.
The general lesson: a blank page with a red console error is the start of a diagnosis, not the end. Read the message, then find the line it points to.
Your Turn: A Second Root
React can manage more than one root on a page. Try it, then decide why apps rarely do.
- In
index.html, add<div id="banner"></div>just above the rootdiv. - In
main.jsx, after the existing call, add a second call that renders a paragraph sayingSeparate rootinto thebannerelement.
Try it before reading the solution.
Solution
// Illustrative fragment: added at the end of src/main.jsx
createRoot(document.getElementById('banner')).render(<p>Separate root</p>)
The paragraph appears above the template's page. Each createRoot call gives
React a separate area to manage. That is why apps normally have exactly one
root. Two roots are two independent React apps: they cannot share state or
pass data to each other through React. Use a second root only when React runs
in a few separate places on a page that something else built. Remove the two
additions before moving on, so first-app matches the template again.
Check Your Understanding
Put these in the order they happen, and say which file each belongs to:
- React puts
App's output on the page. - The browser loads the module script.
- The browser receives the empty root element.
createRootis called with the root element.- The
Appfunction runs.
Answer and Reasoning
- The browser receives the empty root element (
index.html). - The browser loads the module script (
index.htmlnames/src/main.jsx). createRootis called with the root element (main.jsx).- The
Appfunction runs (App.jsx, called by React afterrender). - React puts
App's output on the page (inside<div id="root">).
If you put App running before createRoot, look again at main.jsx:
importing App only makes the function available. React calls it after
render is asked to display <App />.
Reset and Clean Up
Make sure index.html has <div id="root"></div> again and that main.jsx
matches the template. Stop the dev server with Ctrl+C. Keep first-app;
Components and JSX uses
it.
References
- React: createRoot and StrictMode
- Vite: Getting Started (index.html as the entry) and
Static Asset Handling (
public/) - npm: npm run