The Web FrontendDeeper

Kayavak / The Web Frontend / FE·22 React / Deeper

Deeper · The Web Frontend

React

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Inside Facebook around 2011, engineers struggled with interfaces where many parts changed in response to each other (the notification count, chat, the news feed). Jordan Walke built a prototype that re-rendered components from scratch on every change and compared the result with the previous one, applying only the differences. It was released as React in 2013 and initially met scepticism, mostly about JSX mixing markup into JavaScript.

React then changed itself several times. React 16 (2017) rewrote the internals as Fiber, so rendering can be split into pieces and interrupted. Hooks (React 16.8, February 2019) replaced class components with functions that use state and effects, and spread the idea of composable stateful logic. React 18 (2022) made rendering concurrent, so urgent updates can interrupt slow ones. React 19 (2024) brought Server Components and Actions into the stable release, and the React Compiler automates the memoisation people used to write by hand.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Each render is a snapshot: the component runs with the props and state of that moment and returns a description of the screen. React compares snapshots and commits the differences; effects then synchronise anything outside React.

Question Answer
What is it? A library for building UIs from components that are functions of props and state
Problem it solves Keeping complex interactive screens correct as state changes, by re-describing rather than patching
Parts Components and JSX, state (useState, useReducer), effects, refs, context, the reconciler, Suspense and transitions
How it works A state update schedules a render; React calls components, diffs the result (reconciliation), commits DOM changes, then runs effects
Assumptions Rendering is pure; state is updated immutably; effects declare their dependencies
When they change Mutating state in place skips re-renders; side effects in render run twice in development and unpredictably in concurrent mode
Where it fails Large trees re-rendering on every keystroke; effects used for derived data, causing extra renders and bugs
Related ideas UI = f(state) (ch21); state management (ch25); server rendering (ch24)

What if?

What if you mutate an array in state instead of replacing it?

Reason it through, then open

items.push(x); setItems(items) passes React the same array. It compares the old and new values by identity, sees no change, and may skip the re-render. Even when something else causes a render, memoised children comparing props by identity won't update. Creating a new array ([...items, x]) is what tells React something changed.

What if an effect's dependency list is wrong?

Reason it through, then open

Leave a dependency out and the effect keeps using the value from an old render: a stale closure, like a chat subscription stuck on the previous room. Add an object that's recreated every render and the effect runs after every render, perhaps in a loop if it sets state. The lint rule for hook dependencies exists because both mistakes are easy and silent.

Where the boundary is

Aspect State Ref
Changing it Schedules a re-render Doesn't re-render
Read during render Yes: it's the snapshot Shouldn't be: it may change between renders
Use for Anything shown on screen Timer ids, DOM nodes, values you need between renders but don't display

How sure are we?

Claim How it's known Where from
React was open-sourced in 2013; hooks in 16.8, February 2019 Historical React's release notes
Strict Mode renders components twice in development Convention React documentation; to expose impure renders
React re-renders a component when its parent re-renders, by default Convention Unless memoised; the compiler changes when this matters
Most React performance problems come from re-rendering too much Model Common experience and the chapter's lab; not a measured law

Still open

Server Components are the most debated part of modern React: they move work to the server and shrink bundles, but blur where code runs and depend on framework support. How widely they'll be adopted outside Next.js is open.

What would change your mind? If explicit memoisation stopped mattering everywhere because the compiler handled it, much of the chapter's performance advice would become history. That's the compiler's goal.

Recall

Why must React state be updated immutably?

React detects changes by identity; mutating in place passes the same object, so updates can be skipped.

What is a stale closure in an effect?

An effect using values from an old render because a dependency was left out of its list.

What's the difference between state and a ref?

Changing state re-renders; changing a ref doesn't.

What did Fiber (React 16) make possible?

Splitting rendering into interruptible units, the basis for concurrent rendering.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.