Learn / Frameworks / React / JSX and Components

React · Lesson 2 of 7

JSX and Components

Write JSX, compose components and pass data down with props.

  • Beginner
  • 15 min read
  • 3 objectives

Before this lessonLesson 1: Introduction and Setup

What you will learn

  • Write valid JSX
  • Pass and destructure props
  • Use children

JSX is a syntax that lets you write HTML-like markup inside JavaScript. A build tool converts it to ordinary function calls. It is not a template language: it is JavaScript, so you have the full language available inside curly braces.

JSX rules

  • A component returns one root element. Wrap siblings in a <div> or a fragment <>...</>.
  • Close every tag, including <img /> and <br />.
  • Use className instead of class, and htmlFor instead of for.
  • Attributes are camelCase: onClick, tabIndex.
  • Put JavaScript expressions in { }. Style takes an object: style={{ color: "red" }}.
function Profile() {
  const user = { name: "Ada", age: 36 };
  const year = new Date().getFullYear();
  return (
    <>
      <h2 className="title">{user.name}</h2>
      <p>Born around {year - user.age}</p>
    </>
  );
}

Props

Props are how a parent passes data to a child, exactly like function arguments. They arrive as one object, so destructure them in the parameter list. Props are read-only: a component must never change its own props.

function Greeting({ name, excited = false }) {
  return <h1>Hello, {name}{excited ? "!" : "."}</h1>;
}

function App() {
  return (
    <>
      <Greeting name="Ada" excited />
      <Greeting name="Linus" />
    </>
  );
}

Strings can be written in quotes; everything else (numbers, booleans, arrays, objects, functions) goes in braces: <Card count={3} tags={["a", "b"]} />.

Children and composition

Anything placed between a component's tags arrives as the special children prop, letting you build wrapper components.

function Card({ title, children }) {
  return (
    <section className="card">
      <h3>{title}</h3>
      {children}
    </section>
  );
}

<Card title="Tip">
  <p>Small components are easier to reuse.</p>
</Card>;

Conditional rendering

function Status({ online, unread }) {
  if (!online) return <p>Offline</p>;
  return (
    <p>
      Online {unread > 0 && <strong>({unread} new)</strong>}
    </p>
  );
}
// Write your solution here
Up next · Lesson 3State with useStateGive components memory and update it without mutating.