Skip to the content

Lesson 158 of 296

What React is; components and JSX

What you will learn

  • What React is and the problem it solves
  • Declarative vs imperative UI
  • Components
  • JSX and its rules
  • What JSX turns into
  • Starting a React project
On this page

What is React?

React is a JavaScript library for building user interfaces, created by Meta. You describe what the screen should look like for the current data, and React works out how to update the real page (the DOM, Lesson 87) efficiently.

In the plain JavaScript to-do list (Lesson 91) we kept data in a state array and wrote a render() function that rebuilt the list. React is built around exactly that idea: UI = a function of state. React does the rendering and the efficient DOM updates for you.

Declarative vs imperative

Imperative (plain DOM)Declarative (React)
"Find the counter element, change its text to 5, add the class active""When count is 5, the counter shows 5 and has the class active"
You write every step of every changeYou describe the result; React makes the changes
Easy to get out of sync as the app growsThe screen always matches the data

Components

A component is a reusable piece of UI: a button, a card, a whole page. In modern React, a component is a function that returns JSX.

TSX
function Welcome() {
  return <h1>Hello, world</h1>;
}

Rules:

  • Component names start with a capital letter: Welcome, not welcome. Lowercase names are treated as HTML tags.
  • Use a component like an HTML tag: <Welcome />.
  • Components can contain other components, forming a tree (like the DOM).
TSX
function Header() {
  return <header><h1>JobPrep</h1></header>;
}

function Footer() {
  return <footer>© 2026 JobPrep</footer>;
}

function Page() {
  return (
    <div>
      <Header />
      <main>Lessons go here</main>
      <Footer />
    </div>
  );
}

JSX

JSX is the HTML-like syntax inside JavaScript. It is not HTML and not a string: a build tool turns it into JavaScript function calls.

TSX
const heading = <h1 className="title">Hello</h1>;

becomes, roughly:

const heading = jsx("h1", { className: "title", children: "Hello" });

(Older setups used React.createElement.) The result is a plain object describing the element. That is why you can store JSX in variables, return it from functions and put it in arrays.

JSX rules

RuleExample
Return one root element; use a fragment to group without an extra div<>...</>
className instead of class<div className="card">
htmlFor instead of for<label htmlFor="email">
Attributes are camelCaseonClick, tabIndex, aria-label stays as it is
Every tag must close<img />, <br />, <input />
JavaScript goes inside { }<p>{user.name}</p>
style takes an object<div style={{ marginTop: 8 }}>
Comments{/* a comment */}
TSX
function Profile() {
  const user = { name: "Sam", avatarUrl: "/sam.png", isAdmin: true };
  return (
    <>
      <img src={user.avatarUrl} alt={`${user.name}'s profile photo`} />
      <h2>{user.name.toUpperCase()}</h2>
      <p>{user.isAdmin ? "Administrator" : "Member"}</p>
    </>
  );
}

Inside { } you can put any expression (something that produces a value): variables, maths, function calls, ternaries. You cannot put statements like if or for directly; use a ternary, && or map (Lessons 161 and 162), or prepare values before the return.

Safe by default

Text inside { } is escaped automatically: <p>{userInput}</p> shows any HTML tags as text. This protects against XSS (Lesson 87). The escape hatch dangerouslySetInnerHTML exists, and its name is a warning: only use it with sanitised HTML.

Starting a React project

ToolUse
Vite (npm create vite@latest, choose React + TypeScript)A fast setup for single-page apps
Next.js (npx create-next-app@latest)A full framework with routing and server rendering (Part 13). The React team recommends starting new apps with a framework.

A React app mounts into one DOM element:

TSX
import { createRoot } from "react-dom/client";

function App() {
  return <h1>My app</h1>;
}

function mountApp(container: HTMLElement): void {
  createRoot(container).render(<App />);
}

How to say it in an interview

"React is a library for building UIs declaratively: components are functions that return JSX describing the UI for the current state, and React efficiently updates the DOM to match. JSX is syntax that compiles to function calls creating plain objects, so it can be stored and returned like any value. It needs one root or a fragment, uses className and camelCase props, and escapes text by default, which protects against XSS."

Key words

WordMeaning
ReactA library for building UIs from components
DeclarativeDescribing the result rather than each step
ComponentA function that returns UI
JSXHTML-like syntax that compiles to JavaScript
Fragment<>...</>: groups elements without an extra DOM node
MountPut a component on the page for the first time

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.