Skip to content

Hello, React

The Course
•
15 min study

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.

Your first React app React · 3 files
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Hello, React</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html> import { createRoot } from "react-dom/client"; import App from "./App.jsx"; const root = createRoot(document.getElementById("root")); root.render(<App />); export default function App() { return ( <main> <h1>Hello, React!</h1> <p>This page was built by a JavaScript function.</p> </main> ); }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Press Run if the preview is empty. Then open each file in this order.

  1. index.html is 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 that div.

  2. src/main.jsx is the starting point. createRoot takes the empty div and hands it to React. root.render(<App />) tells React to draw the App component inside it:

    JSX
    import { createRoot } from "react-dom/client";
    import App from "./App.jsx";
    
    const root = createRoot(document.getElementById("root"));
    root.render(<App />);
  3. src/App.jsx holds a function called App. It returns something that looks like HTML. That is JSX, and it describes what App should show:

    JSX
    export 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! in App.jsx to Hello, Ada! and press Run, what changes in the preview?
  • If you change the text inside the <title> tag in index.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

Exercise

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.

Your working space React · 3 files
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Hello, React</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html> import { createRoot } from "react-dom/client"; import App from "./App.jsx"; const root = createRoot(document.getElementById("root")); root.render(<App />); export default function App() { return ( <main> <h1>Hello, React!</h1> <p>This page was built by a JavaScript function.</p> </main> ); }
Runs with Vite on your computer: unzip, then 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
JSX
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.

Was this article helpful?

Your response is saved on this device.