What React is: components and rendering

Intermediate

Introduction

In the JavaScript course you changed pages by hand: find an element, change its text, add a class, remember to undo it later. That works for small pages, but in an app with hundreds of moving parts it becomes hard to keep the page and the data in step. React takes a different approach. You write components that describe what the page should look like for the current data, and React does the DOM work: when the data changes, it runs your components again and changes only what is different.

React was created at Facebook and is now the most widely used library for building web interfaces. This course uses React 19, the current version, running in your browser.

How it works

A component is a JavaScript function whose name starts with a capital letter and that returns JSX, the HTML-like syntax React uses:

function Greeting() {
  return <h1>Hello, React!</h1>;
}

Components are used like tags inside other components: <Greeting />. A page is a tree of components, with one at the top. In this course, that top component is called App, and every program exports it:

export default function App() {
  return (
    <main>
      <Greeting />
    </main>
  );
}

A real React project starts with a few lines that tell React which element of the HTML page it controls, the root, and what to show in it:

import { createRoot } from "react-dom/client";
createRoot(document.getElementById("root")).render(<App />);

Here StepVioz does that part for you: it compiles your code, then renders your App into the preview.

When React shows a component, it renders it: it calls the function and reads the JSX it returns. Later, when the component's data changes, React calls the function again, compares the new result with the old one, and updates only the parts of the page that changed. You never write "find this element and change it"; you write "this is what it looks like", and React keeps the page true to that.

Example

App.jsx
console.log output from the page shows here.

Line by line

  1. Greeting and Note are components: functions with capital names that return JSX.
  2. App returns a main element that contains the other components.
  3. <Note /> is used twice, so its paragraph appears twice: a component is a reusable piece.
  4. export default marks App as the program's main component, the one StepVioz renders.

Expected result

The preview shows the heading "Hello, React!" and, under it, the paragraph "This page is built from components." twice.

In the real world

Facebook, Instagram, WhatsApp Web, Netflix and Airbnb build their interfaces with React, and React Native uses the same components to build mobile apps. A large app has thousands of components, from small ones such as a button or an avatar to whole pages, all combined the same way as in this lesson.

Common mistakes

  • Starting a component's name with a lower-case letter: React treats <greeting /> as an HTML tag, not your component.
  • Calling a component like a function (Greeting()) instead of using it as a tag.
  • Forgetting to return the JSX from the component, so nothing is shown.
  • Defining a component inside another component, which makes React treat it as new on every render.

Best practices

  • Keep each component small, with one clear job.
  • Name components after what they show: ProductCard, SearchBox, Header.
  • Define components at the top level of the file, never inside other components.

Summary

React builds interfaces from components, functions with capital names that return JSX describing what to show. Components are combined like tags into a tree with App at the top, which React renders into the page's root element. When data changes, React renders the components again and updates only what changed.

Quick check

1. What is a React component?
2. Why must a component's name start with a capital letter?
3. What does React do when a component's data changes?
4. How is the Note component shown twice?

Key takeaways

  • A React app is a tree of components, each a function that returns JSX.
  • You describe what the page should look like; React works out which DOM changes make it so.
  • Component names start with a capital letter, and App is the top of the tree.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.