React fundamentals

Components, props, state, effects and the rendering model, with the patterns that keep components predictable.

L1 · Junior engineer≈ 5 min read
Key points · the two-minute version
  1. UI is a function of props and state. Rendering must be pure.
  2. Update state immutably ([...items, x]), never by mutating.
  3. Effects sync with the outside world. Don't use them to derive state; compute it during render.
  4. Use a single status or a discriminated union instead of contradictory booleans.
  5. Give lists stable keys, not indexes, and handle fetch races.
You should be able to
  • Build components with props and state
  • Use effects correctly (and avoid them when you can)
  • Model UI state so impossible states can't happen

Why this matters

React is the most widely used UI library and the default for AI product frontends. Most React bugs come from misunderstanding when components re-render and what effects are for.

Core ideas

  • Components are functions that return UI from props (inputs) and state (memory).
  • Rendering: when state or props change, React calls the component again and updates the DOM to match. Rendering should be pure: no side effects.
  • State updates are asynchronous and immutable: setItems([...items, item]), never items.push(item).
  • Effects (useEffect) synchronise with things outside React: subscriptions, timers, non-React widgets. They are not for deriving data from props or state.
  • Keys identify list items across renders; use stable IDs, not array indexes.
  • Lifting state up: shared state lives in the closest common parent; context for truly global things (theme, current user).

How to do it

type Status = "idle" | "loading" | "error" | "done";

function AskBox({ onAsk }: { onAsk: (q: string) => Promise<string> }) {
  const [question, setQuestion] = useState("");
  const [status, setStatus] = useState<Status>("idle");
  const [answer, setAnswer] = useState("");

  async function submit(e: React.FormEvent) {
    e.preventDefault();
    setStatus("loading");
    try { setAnswer(await onAsk(question)); setStatus("done"); }
    catch { setStatus("error"); }
  }

  return (
    <form onSubmit={submit}>
      <textarea value={question} onChange={(e) => setQuestion(e.target.value)} />
      <button disabled={status === "loading" || !question.trim()}>Ask</button>
      {status === "error" && <p role="alert">Couldn't get an answer. Try again.</p>}
      {status === "done" && <p>{answer}</p>}
    </form>
  );
}

Derived values (like "is the button disabled") are computed during render, not stored in state or synced with effects.

Why this way

Pure rendering + immutable updates make components predictable and fast to diff. One status value (or a discriminated union) instead of several booleans prevents contradictory states like loading && error.

Common mistakes

  • useEffect to copy props into state, causing stale or doubled renders.
  • Missing dependencies in effect arrays (stale closures).
  • Index keys in lists that reorder, mixing up item state.
  • Fetching in effects without handling races (the slower old request overwriting the newer one). Use a data library or abort controllers (4.4).

Interview follow-ups

  • "Why does my component render twice in development?" React Strict Mode intentionally double-invokes renders and effects to expose impure code; production renders once.
  • "When do you reach for useMemo / useCallback?" When profiling shows an expensive computation or a memoised child re-rendering needlessly; not by default (React's compiler increasingly handles this).
  • "Context or a state library?" Context for low-frequency global values (theme, user); a store or a server-state library for frequently changing or server data, to avoid re-rendering the whole tree.
  • "How do you test components?" Testing Library from the user's perspective (roles, text, events), mocking the network (MSW), not implementation details.

Check yourself

When should you use `useEffect`?

To synchronise with something outside React (subscriptions, timers, imperative widgets, browser APIs). Not to compute values from props or state; compute those during render.

Why not store `isLoading`, `isError` and `data` as three separate states?

They can contradict each other. A single status (or a discriminated union with data attached) makes impossible states unrepresentable.

Build it

Build the AskBox with a fake onAsk that sometimes fails, and Testing Library tests for loading, error and success.

What comes next

4.3 Next.js and the App Router.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

Kanban board

Columns, drag to move, add/edit cards, undo; state in a reducer with tests for every action.

  • React
  • TypeScript
  • Vite
  • Testing Library

Practises State modelling, immutability, testing UI logic.

Project 2

Ask box with status union

A question form calling a mocked API that sometimes fails or is slow, with idle/loading/error/success states and race-safe requests.

  • React
  • TypeScript
  • MSW

Practises Async UI states and race conditions.

Can you answer every check-yourself question out loud without opening the answer?