Sub topic 12
How index.html and main.jsx start the app
This chapter follows what happens from the moment the browser requests your page to the moment the first component appears on screen. It assumes you can scaffold a Vite project, you know what a component is, and you are comfortable with ES modules (import and export).
The two files that start everything
A Vite React project has two files that act as the entry point.
index.htmlsits at the project root, not inpublic/orsrc/. It is the page the browser loads.src/main.jsxis the first JavaScript module that runs. It connects React to the page.
Everything else in src/ is reached through imports that begin in main.jsx.
index.html: the page shell
The generated index.html looks like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>Two lines matter most.
<div id="root"></div>is an empty container. React will fill it. Until JavaScript runs, the page body is blank, which is why a plain Vite single-page app shows nothing with JavaScript disabled.<script type="module" src="/src/main.jsx"></script>loads your entry module. Thetype="module"attribute tells the browser to treat the file as an ES module, soimportstatements work.
The script path starts with /. In Vite, that means "from the project root", so /src/main.jsx is the file src/main.jsx in your project.
main.jsx: connecting React to the page
The entry file from the template:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)Read it in three parts.
Imports
StrictModecomes from thereactpackage. It is a component that adds extra development checks.createRootcomes fromreact-dom/client. React itself is independent of the browser. Thereact-dompackage is the part that knows how to draw React output into the browser's DOM (Document Object Model, the browser's tree of page elements).Appis your top-level component, imported from./App.jsx. Note the explicit.jsxextension. Vite resolves it as written.
Creating a root
document.getElementById('root') returns the <div id="root"> from index.html. createRoot takes that DOM element and returns a root object. A root is the link between one DOM element and one React component tree. React manages everything inside that element from then on.
Rendering
root.render(...) tells React what to show. Here it shows <App /> wrapped in <StrictMode>. The first call draws the initial UI. Later updates, such as a state change, re-render the affected components without you calling render again.
What the browser does, step by step
The sequence below shows the full start-up path in development.
- The browser requests
/. The dev server returnsindex.html. - The browser parses the HTML and finds the module script. It requests
/src/main.jsx. - Vite transforms the JSX into plain JavaScript and returns it. Browsers cannot run JSX directly.
- The browser reads the
importlines in that module and requests each imported file. Vite serves them one by one, transforming as needed. Packages fromnode_modules, such asreact, are rewritten to paths the browser can load. - Once the modules are loaded,
main.jsxruns. React builds the UI fromAppand inserts DOM nodes intodiv#root.
In a production build, the same logic applies, but the many modules are bundled into a few files and the script tag in the built index.html points at a hashed filename.
Why StrictMode exists
StrictMode does not render any visible UI. In development it:
- Renders each component twice to expose code that is not pure, such as code that changes a variable outside the component while rendering.
- Re-runs effects once after mounting to expose missing cleanup.
- Warns about deprecated APIs.
None of this happens in a production build. A common surprise is a console.log inside a component that prints twice in development. That is StrictMode working as intended, not a bug in your code.
You can remove the wrapper and render <App /> directly. Doing so hides problems instead of fixing them, so keep it unless you have a specific reason.
Common errors at the entry point
| Symptom | Likely cause | Fix |
|---|---|---|
| Blank page, console says `Cannot read properties of null` or `Target container is not a DOM element` | `getElementById` found nothing. The id in `main.jsx` does not match the id in `index.html`. | Make both use the same id, such as `root`. |
| Blank page, 404 for `/src/main.jsx` | The script `src` path is wrong, or the file was renamed. | Match the path to the real file. |
| `Failed to resolve import "./App.jsx"` | The file name or case differs. On Linux, `app.jsx` and `App.jsx` are different files. | Fix the import path and casing. |
| Syntax error on `<App />` in a `.js` file | JSX in a file without a `.jsx` extension. The React plugin expects `.jsx` or `.tsx` for JSX. | Rename the file to `.jsx`. |
| Component shows default export is undefined | `App.jsx` lacks `export default App`. | Add the default export, or use a named import. |
A minimal experiment
To see that main.jsx is just ordinary JavaScript, replace its contents with a version that has no App file at all:
import { createRoot } from 'react-dom/client'
function Hello() {
return <h1>Rendered straight from main.jsx</h1>
}
createRoot(document.getElementById('root')).render(<Hello />)Save it while the dev server runs. The page updates and shows the heading. App.jsx is a convention of the template, not a requirement. Restore the original file afterwards so later chapters match your project.
Edge cases
- One root per container. Calling
createRoottwice on the same element produces a warning. Create the root once and callrenderon it again if you need to replace the tree. - Multiple roots on one page. You can create several roots on different elements. This is how you add React to part of an existing page. Each root has its own independent tree.
- Scripts in the head. Module scripts are deferred by default, so they run after the HTML is parsed. That is why
div#rootexists by the timemain.jsxruns, even if you move the script tag. - Top-level
awaitand other modern syntax. Vite's dev server serves modern JavaScript as written. The production build targets a set of browsers chosen in its config, covered in a later chapter.
Exercise
Your page is blank and the console reports that the render target is not a DOM element. main.jsx contains document.getElementById('app') and index.html contains <div id="root"></div>. Name two ways to fix this.
Show answer
Either change main.jsx to call document.getElementById('root'), or change the div in index.html to <div id="app"></div>. The id in both files must match, because getElementById returns null when no element has that id, and createRoot cannot use null.
Exercise
A component logs "rendered" to the console, and the message appears twice on each page load in development but only once in a production build. What causes this, and does it indicate a bug?
Show answer
StrictMode in main.jsx renders components twice in development to surface impure rendering code. It is not a bug. Production builds do not do the double render.