React

UI = a function of state. That is the whole framework.

▶ the lesson · narrated sketch video · tap for sound

the intro

React is a JavaScript library for building user interfaces out of small, reusable pieces called components. You describe what the screen should look like for a given state, and React figures out how to update the page when that state changes. Once it clicks, you stop fighting the DOM forever.

the sample

a real component, in full

function Counter() {
  const [n, setN] = useState(0)
  return (
    <button onClick={() => setN(n + 1)}>
      Clicked {n} times
    </button>
  )
}

fun fact ✦

React was born at Facebook in 2011 to tame their own ad-code chaos. Now it runs a huge chunk of the internet.

the docs

the deep dive

the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid

Before React, you wrote the screen twice: once at load, then again as step-by-step DOM surgery on every change. React deletes the second half. You describe what the screen looks like for the current state — a plain function — and the library computes the minimum updates. State changes, screen follows.

describe the screen, not the surgery

setStatere-renderDOM patch
what happens when you call setState

key terms

component

A function that takes props and returns UI. Compose them like functions; reuse them like building blocks.

function Card() { ... }

props

Inputs passed from parent to child, read-only inside. Like function arguments for markup.

<Card title="Hi" />

state

Data a component owns that changes over time. Every update schedules a re-render.

useState(0)

hook

A use-prefixed function that taps into React powers: state, effects, context. Rule: call hooks at the top level only.

useState, useEffect

how it works

  1. 1
    Break the UI into components

    One piece per job: Card, Badge, SearchBar. If you can name it, it can be a component.

  2. 2
    Find the minimal state

    What actually changes over time? Compute everything else during render instead of storing it.

  3. 3
    Wire events to setState

    Handlers call the setter; React re-renders, diffs the trees, and patches only what changed.

  4. 4
    Lift shared state

    Two siblings need the same data? Move it to their closest common parent and pass it down as props.

the code, explained

a complete component — the whole idea in eight lines

1function Like() {
2 const [liked, setLiked] = useState(false)
local state; the setter schedules a re-render
3 return (
4 <button onClick={() => setLiked(!liked)}>
5 {liked ? "Liked" : "Like"}
the UI is literally a function of state
6 </button>
7 )
8}

side by side

props

  • passed in by the parent
  • read-only inside the child
  • like function arguments
  • changes come from above
VS

state

  • owned by the component
  • private and updatable
  • changes via the setter
  • updates trigger re-render
🪤 gotcha

Using the array index as key works until the list reorders or an item is deleted — then React misidentifies elements and scrambles their internal state: focus jumps, inputs keep old text. Keys are identity, not decoration. Use the stable id from your data.

quick answers

Why not just vanilla JavaScript?

For mostly-static pages, skip React. The moment the UI has lots of changing state, manual DOM syncing becomes your app — React automates exactly that.

What is the virtual DOM, really?

A cheap JavaScript description of your UI. React diffs the new one against the old and touches the real DOM as little as possible.

Do I still need useMemo everywhere?

Less every year — the React Compiler now memoizes automatically. Measure first; sprinkle optimization second.

quick hits about React

short answers · interviews, tips, trivia & hidden gems

🎤 interview

Props or state?watch ↗

🎤 interview

Why does your useEffect loop forever?watch ↗

🎤 interview

What does the virtual DOM do?watch ↗

🎤 interview

Two components need the same data?watch ↗

📚 tutorial

Forms the React way?watch ↗

📚 tutorial

Reuse logic between components?watch ↗

💎 deep

Why not use the index as key?watch ↗

💎 deep

One component crashes everything?watch ↗

🆕 update

Still writing useMemo everywhere?watch ↗

🆕 update

Forms in React 19?watch ↗

🧠 trivia

React had another name?watch ↗