Skip to article content
FrontendXLesson 01 / 40

Components, JSX and Composition

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

beginner · about 45 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Test

What you will be able to do

#
  • Define a typed function component and render it.
  • Pass data with props and compose UI with the children prop.
  • Explain how JSX compiles to function calls in React 19.

Overview

#

What a component is

A React component is a JavaScript function that returns a description of UI, called an element. React calls the function and turns the returned tree into DOM. Components must be named with a capital letter so the JSX compiler treats them as components and not as HTML tags.

snippet.tsx
function Greeting({ name }: { name: string }) {
  return <h1>Hello, {name}</h1>;
}

Greeting is a pure function of its props. Given the same props it returns the same element description, which is what lets React reason about updates safely.

JSX is syntax sugar

JSX is not a template language and not a string. The React 19 automatic runtime compiles <h1>Hello</h1> into a call to jsx("h1", { children: "Hello" }). You do not import React just to use JSX. Expressions go inside braces: {name}, {2 + 2}, {items.map(...)}. Attribute names are camelCase (className, onClick, htmlFor) because they map to DOM properties.

snippet.tsx
const card = <article className="card">Total: {price * quantity}</article>;

Props and children

Props are read-only inputs. A component must never mutate its props. To render nested content, accept the children prop, which React passes automatically for anything between the opening and closing tags.

snippet.tsx
type PanelProps = { title: string; children: React.ReactNode };

function Panel({ title, children }: PanelProps) {
  return (
    <section>
      <h2>{title}</h2>
      {children}
    </section>
  );
}

<Panel title="Profile">
  <p>Ada Lovelace</p>
</Panel>

Composition through children keeps components decoupled: Panel controls layout, the caller controls content.

Conditional rendering

Use ordinary JavaScript. Ternary for either/or, logical AND for show/hide, and early returns for guard clauses.

snippet.tsx
function Status({ online }: { online: boolean }) {
  if (!online) return <p>Offline</p>;
  return <p>Online</p>;
}

Guard against the classic pitfall of count && <Badge />: when count is 0, React renders the number 0. Write count > 0 && <Badge /> instead.

Lists need keys

When you render an array, each element needs a stable key so React can match items across renders.

snippet.tsx
<ul>
  {users.map((user) => (
    <li key={user.id}>{user.name}</li>
  ))}
</ul>

Use a stable identity such as a database id, never the array index when the list can reorder, insert or delete.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What does this component render when count is 0?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    function Badge({ count }: { count: number }) {
      return <div>{count && <span>New</span>}</div>;
    }
    
    <Badge count={0} />
    Choose the best answer
  2. Exercise 2Implement

    Implement a typed Card component that accepts a title string and arbitrary children, and renders both inside an article element.

    starter.tsx
    type CardProps = {
      title: string;
      children: React.ReactNode;
    };
    
    export function Card(props: CardProps) {
      return null;
    }

    Reveal the reference answer
    reference.tsx
    type CardProps = {
      title: string;
      children: React.ReactNode;
    };
    
    export function Card({ title, children }: CardProps) {
      return (
        <article>
          <h2>{title}</h2>
          {children}
        </article>
      );
    }

    Authored expected output: Renders an article with an h2 showing the title and the passed children below it.

    Destructure title and children from props and place children where the content should appear. children is typed as React.ReactNode to accept any renderable content.

  3. Exercise 3Explain

    Explain why the array index is a poor key for a sortable list, and describe what React does when keys change.

    starter.tsx
    items.map((item, index) => <Row key={index} item={item} />)

    Reveal the reference answer
    reference.tsx
    Index keys tie identity to position, not to the item, so after a sort or deletion React matches the wrong element state to the wrong item. When keys change, React treats the element as a different component instance, unmounts the old one and mounts a new one, discarding local state and DOM state such as focus.

    Authored expected output: A written explanation of identity versus position and the unmount/mount consequence.

    Keys are React's identity signal during reconciliation. A stable item id lets React move the existing DOM node and keep its state; an index key makes reordering look like in-place edits, which can carry stale input values or focus to the wrong row.

  4. Exercise 4Test

    Write a test description for Greeting that proves it renders the name from props. State the assertion and the expected DOM.

    starter.tsx
    function Greeting({ name }: { name: string }) {
      return <h1>Hello, {name}</h1>;
    }

    Reveal the reference answer
    reference.tsx
    Render <Greeting name="Ada" /> and assert that the heading role with level 1 has the accessible name "Hello, Ada".

    Authored expected output: One assertion: getByRole("heading", { level: 1 }) has text content "Hello, Ada".

    Query by role and level rather than by tag or text fragment so the test also guards the semantic structure. Assert the full accessible name to prove props flow into output.

Common misconceptions

#
  • JSX is HTML. It is not: attributes are camelCase DOM properties and expressions must be wrapped in braces.
  • You must import React in every file. With the React 19 automatic runtime you do not.
  • Props can be mutated inside a component. Props are read-only and mutation breaks rendering.
  • Using the array index as a key is always fine. It breaks when the list reorders or changes length.

Follow-up practice

#
  • react-rendering-and-reconciliation
  • react-state-and-updates

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.