TExcelTAD
Home › React › React First App

React First App

Let's build a small but complete app: a greeting card maker. Along the way you will meet every piece you'll use in later lessons — components, JSX, props, state and events — so that the following chapters feel familiar.

Step 1: One component

Every app starts with a root component, conventionally called App. It is a function whose name starts with a capital letter and returns JSX:

jsx · try it yourself
export default function App() {
  return (
    <div>
      <h1>Greeting Card Maker</h1>
      <p>Happy birthday, Linh!</p>
    </div>
  );
}

Rules you already followed without noticing:

  • The function name is capitalized (App, not app). Lowercase names are treated as HTML tags.
  • The function returns one outer element (here a <div>).
  • Multi-line JSX is wrapped in parentheses after return.

Step 2: Split it into components

The card itself deserves its own component. We pass it data through props — attributes on the tag that arrive as an object parameter:

jsx · try it yourself
function Card({ to, message }) {
  return (
    <div style={{ border: '2px dashed hotpink', borderRadius: 12, padding: 16, maxWidth: 280 }}>
      <h2 style={{ margin: 0 }}>Dear {to},</h2>
      <p>{message}</p>
    </div>
  );
}

export default function App() {
  return (
    <div>
      <h1>Greeting Card Maker</h1>
      <Card to="Linh" message="Happy birthday! 🎂" />
      <Card to="Minh" message="Congratulations on the new job! 🎉" />
    </div>
  );
}

Curly braces { } switch from markup back into JavaScript. {to} inserts the value of the to variable.

Step 3: Add state and events

Now let the user type the name and pick a message. Values that change over time are stored with the useState hook. Every time a state setter is called, React re-runs the component and updates the screen:

jsx · try it yourself
import { useState } from 'react';

const MESSAGES = ['Happy birthday! 🎂', 'Get well soon! 🌷', 'Congratulations! 🎉', 'Thank you! 🙏'];

function Card({ to, message }) {
  return (
    <div style={{ border: '2px dashed hotpink', borderRadius: 12, padding: 16, maxWidth: 280, marginTop: 12 }}>
      <h2 style={{ margin: 0 }}>Dear {to || '…'},</h2>
      <p>{message}</p>
    </div>
  );
}

export default function App() {
  const [name, setName] = useState('Linh');
  const [index, setIndex] = useState(0);

  function nextMessage() {
    setIndex((index + 1) % MESSAGES.length);
  }

  return (
    <div>
      <h1>Greeting Card Maker</h1>
      <label>
        Name: <input value={name} onChange={(e) => setName(e.target.value)} />
      </label>{' '}
      <button onClick={nextMessage}>Next message</button>
      <Card to={name} message={MESSAGES[index]} />
    </div>
  );
}

What happens when you type a letter:

  1. The input fires onChange, and the handler calls setName with the new text.
  2. React schedules a re-render of App with the new name.
  3. App returns new JSX, including <Card to={name} /> with the updated prop.
  4. React compares the new tree with the previous one and changes only the text that differs.

Step 4: Add a list

Let's keep a list of sent cards. Arrays of data are turned into arrays of elements with .map(), and each item needs a unique key:

jsx · try it yourself
import { useState } from 'react';

export default function App() {
  const [name, setName] = useState('');
  const [sent, setSent] = useState([]);

  function send() {
    if (!name.trim()) return;
    setSent([...sent, { id: sent.length + 1, to: name.trim() }]);
    setName('');
  }

  return (
    <div>
      <h1>Card Outbox</h1>
      <input value={name} placeholder="Recipient" onChange={(e) => setName(e.target.value)} />
      <button onClick={send}>Send card</button>
      <p>{sent.length === 0 ? 'No cards sent yet.' : `${sent.length} card(s) sent:`}</p>
      <ul>
        {sent.map((card) => (
          <li key={card.id}>💌 Card #{card.id} to {card.to}</li>
        ))}
      </ul>
    </div>
  );
}

Notice that we never modify the sent array. We build a new array with the spread syntax [...sent, newItem]. React detects changes by comparing references, so state must be replaced, not mutated.

What you used

Concept Where you saw it Lesson
Component function Card() Components
JSX <h2>Dear {to},</h2> JSX
Props <Card to="Linh" /> Props
State useState('Linh') useState
Events onClick={nextMessage} Events
Lists sent.map(...) with key Lists & Keys

Tip: When something doesn't update, ask yourself: "Is this value in state, and am I calling the setter with a new value?" That question solves most beginner bugs.

Test yourself

Why must a component name start with a capital letter?

  1. It's only a style convention
  2. Lowercase tags are treated as built-in HTML elements
  3. Lowercase names are reserved for hooks
  4. Otherwise the component can't receive props

What happens after you call a state setter such as setName?

  1. Nothing until the page reloads
  2. React re-renders the component with the new value
  3. The DOM is replaced completely
  4. The browser navigates to a new URL

Which line correctly adds an item to an array in state?

  1. sent.push(item)
  2. setSent(sent.push(item))
  3. setSent([...sent, item])
  4. sent = [...sent, item]