React is a JavaScript library for building user interfaces. Instead of writing a page as one long HTML file, you write small JavaScript functions that each describe a piece of the page. React calls those functions and puts what they describe on the screen. When something changes later, such as a click or some typing, React works out which parts of the page to update.
This course assumes you know JavaScript basics: variables, functions, arrays and objects. You don't need to know much HTML. Each lesson explains the HTML it uses.
Your first React app
Below is a complete React app. It has three files, and the tabs above the
editor switch between them. The app runs in the preview, and anything it prints
with console.log appears in the console under the preview. On a wide screen
the preview sits beside the code; here it's underneath. The window button at the
top right opens the lab in a window of its own, and you can drag the dividers
between the sections to resize them.
npm install and npm run dev. Press Run if the preview is empty. Then open each file in this order.
-
index.htmlis the page the browser loads. Its body has one empty element,<div id="root"></div>, and a script tag that loads/src/main.jsx. Everything React draws goes inside thatdiv. -
src/main.jsxis the starting point.createRoottakes the emptydivand hands it to React.root.render(<App />)tells React to draw theAppcomponent inside it:JSXimport { createRoot } from "react-dom/client"; import App from "./App.jsx"; const root = createRoot(document.getElementById("root")); root.render(<App />); -
src/App.jsxholds a function calledApp. It returns something that looks like HTML. That is JSX, and it describes whatAppshould show:JSXexport default function App() { return ( <main> <h1>Hello, React!</h1> <p>This page was built by a JavaScript function.</p> </main> ); }
So the path is: the browser loads index.html, which runs main.jsx, which
asks React to show App. You will see this same three-step layout in almost
client-side Vite React project. Frameworks can start and render an app
differently; this course uses Vite so you can see the entry point directly.
Files ending in .jsx
A .jsx file is JavaScript that is allowed to contain JSX. Browsers can't run
JSX directly, so a tool translates it into ordinary JavaScript first. The lab
does that translation for you each time you press Run.
Predict, then change
Before you edit anything, write down your answers to these two questions:
- If you change
Hello, React!inApp.jsxtoHello, Ada!and press Run, what changes in the preview? - If you change the text inside the
<title>tag inindex.html, will you see it in the preview?
Now try both.
Check your predictions
The heading changes to Hello, Ada! because App now returns different JSX.
The title doesn't show up in the preview. <title> sets the text of a browser
tab, and the preview is a frame inside this page, not a tab. In a real project
you would see it in the tab.
Press Reset whenever you want the original files back. You can also press Ctrl + Enter (or ⌘ + Enter on a Mac) in the editor to run without reaching for the mouse. Download project under the lab saves your version as a project you can run on your own computer with Vite.
When the root can't be found
main.jsx looks up the page element by its id. Change
document.getElementById("root") to document.getElementById("app") and run
the app. Before you look, predict: will you see an error, a blank preview, or
both?
Read the error
The preview stays blank, and the console reports
src/main.jsx:4: Uncaught Error: Target container is not a DOM element. getElementById("app") found nothing and returned null,
so React had nowhere to draw. The fix is to make the id in main.jsx match the
id in index.html. Change it back, or change both so they match.
When an app shows nothing, open the console first. React's messages usually name the problem.
Your turn
Make it yours
Change the app so it shows:
- a heading with your name, such as Hello, Ada!
- a paragraph saying what you want to build with React
- a second paragraph saying how many minutes a day you plan to study
Edit only src/App.jsx. Keep the main element, and put both paragraphs
inside it, after the heading.
npm install and npm run dev. Hint 1: the idea
Everything App shows is in the JSX after return. New elements go between
<main> and </main>.
Hint 2: a first step
Copy the existing <p>...</p> line, paste it on the next line, and change the
text. Run it before you add the third element.
Show one solution
export default function App() {
return (
<main>
<h1>Hello, Ada!</h1>
<p>I want to build a study planner.</p>
<p>I plan to study for 45 minutes a day.</p>
</main>
);
}Your text can differ. What matters is that all three elements are inside the
one main element that App returns.
Completion is saved in this browser only. It does not measure mastery.