Skip to content

React

React is a library for describing a UI as a function of state. You do not tell the DOM what to change; you render what the screen should look like for the current data, and React works out the minimal set of DOM operations. Everything else — hooks, keys, memoisation — follows from that inversion.

“A React app is a tree of components. A component is a function that takes props and returns elements, usually in JSX, which compiles to jsx() calls. React keeps a lightweight element tree, compares it with the previous one during reconciliation, and commits only the DOM mutations that differ. Props are read-only inputs from the parent; state is memory owned by the component, updated with a setter that schedules a re-render. Hooks let function components use state, effects and context, and they replace the lifecycle methods that only class components ever had. Separately, rendering can happen on the client, on the server or at build time — that choice is about where the HTML is produced, not about the component model.”

Button.tsx
type ButtonProps = {
label: string;
variant?: 'primary' | 'secondary';
onClick?: () => void;
};
export function Button({ label, variant = 'primary', onClick }: ButtonProps) {
return <button className={`btn btn--${variant}`} onClick={onClick}>{label}</button>;
}

JSX is syntax, not a template language: it compiles to function calls, which is why expressions go in braces, why the attribute is className, and why a component returns one root (a fragment <>...</> avoids an extra DOM node). Component names are capitalised so the compiler can tell a component from an HTML tag.

The virtual DOM is an in-memory description of the UI. On each render React builds a new element tree; the reconciler walks it against the previous tree and produces a change list, which the renderer applies to the host (DOM, native views, a string on the server).

Two heuristics make this fast: a different element type replaces the whole subtree (state included), and keys identify children in a list. Without a stable key React matches by index, so inserting at the front shifts every row and its state.

Props State
Owner Parent The component itself
Mutability Read-only Updated via setter
Change triggers render Yes, when the parent passes new values Yes
Survives remount Yes No

If a value can be computed from props or other state, do not store it — derived state can go stale. State updates are batched and asynchronous: two setCount(count + 1) calls in one handler increment once, because both read the same render’s value. Use the updater form when the next value depends on the previous one.

const [count, setCount] = useState(0);
setCount((c) => c + 1);
setCount((c) => c + 1); // +2
Hook Purpose
useState / useReducer Local state, or state with action-based transitions
useEffect Synchronise with external systems after commit
useLayoutEffect Same, but before paint (measurement, scroll position)
useRef Mutable value that does not re-render; DOM refs
useContext Read a context value
useMemo / useCallback Cache a value or function identity
useTransition / useDeferredValue Keep urgent updates responsive
useId Stable ids for accessibility attributes

Rules of hooks: call them only at the top level of a component or another hook — never in conditions, loops or nested functions — because React matches hooks by call order.

Lifecycle methods are a class component feature. Function components never had them; hooks cover the same needs.

Class lifecycle Function component equivalent
constructor Initial state or a lazy useState initialiser
componentDidMount useEffect(() => {...}, [])
componentDidUpdate useEffect(() => {...}, [deps])
componentWillUnmount The cleanup function returned from useEffect
shouldComponentUpdate memo, useMemo, useCallback
getDerivedStateFromProps Usually: derive during render instead
componentDidCatch An error boundary (still a class, or a framework equivalent)

useEffect is not a lifecycle method. It is a synchronisation mechanism: “while these values are current, keep this external system in sync”. That framing removes most dependency-array confusion, because the array is just when to re-run the synchronisation.

Strategy HTML produced First paint Best for
CSR In the browser Slow; blank until JS loads App shells behind auth
SSR Per request on the server Fast, then hydrates Personalised, dynamic pages
SSG At build time Fastest; CDN-cacheable Docs, marketing, blogs
ISR / streaming At build plus revalidation, or streamed Fast, stale-while-revalidate Catalogues, large content sites

These are orthogonal to the component model: the same component can render on the server and hydrate on the client. What differs is where and when the HTML is generated, and whether the client must download JavaScript to make it interactive. Next.js is covered in Next.js.

  • Managing State — local versus shared state, context, reducers, server state, and when not to reach for a library.
  • Next.js — the App Router, server and client components, layouts, data fetching and caching.
  • Designing UI — composition, accessibility, forms, lists, performance and failure states.
  1. Why does React need keys, and what goes wrong with array indices?
  2. What is the difference between rendering and committing?
  3. Why are state updates batched, and when does that surprise you?
  4. What happens during hydration, and why can it mismatch?
  • “Lifecycle methods run in function components.” They do not; hooks cover those needs.
  • “The virtual DOM is faster than the DOM.” It is fast enough and enables a declarative model; hand-written DOM updates can be faster.
  • “Angular renders on the server by default.” It does not. Angular is a client-side framework; server rendering is an optional add-on, as it is for React.
  • Mutating state directly. state.items.push(x) changes the object without a re-render, because React compares by reference.
  • Storing derived state. Compute it during render instead.