React
Build user interfaces as functions of state: components, props, state, effects and the rules that connect them. You read real components, prove what they render and learn to predict every re-render.
- Intermediate
- 4 units, 12 lessons, 154 exercises
- TypeScript with JSX
- Builds on HTML & CSS Layout, JavaScript; TypeScript in Depth helps
Unit 01 · 3 lessons
Components
Write markup inside TypeScript, split a page into functions that take props, and turn arrays and conditions into elements.
- 01JSX
Markup inside TypeScript: braces for expressions, attributes, one root, and what a tag really is.
- 02Props
Components are functions: props go in, elements come out, and data flows down.
- BOSSLists and conditions
A product list end to end: map, keys that hold, conditions, and the stray 0.
Read the unit guide
A React app is a tree of components, and a component is a function that returns markup. The markup is JSX: tags written inside TypeScript, in a .tsx file. A build step turns every tag into a function call that returns a small plain object (a React element) saying "a p with this class and this text". Nothing touches the page at that point. React reads the objects later and creates the DOM nodes.
Inside JSX, curly braces switch back to JavaScript for one expression: {user.name}, {price * qty}, {ok ? 'yes' : 'no'}. Statements such as if and for do not fit; do that work above the markup. Attributes are DOM property names (className, htmlFor, tabIndex), every tag must be closed, a string attribute goes in quotes and any other value in braces. An expression has one root, so wrap siblings in a Fragment, <> and </>, when you want no extra element.
A component's name starts with a capital letter; a lowercase tag means a built-in HTML element. The attributes on its tag arrive as one object, the props, and whatever sits between its tags arrives as the children prop. Type the props, and a missing or wrong one becomes a compile error. Props are read-only and travel one way, parent to child, one level at a time.
Lists come from map: turn each item of the data into an element, and give each one a key, a stable id from the data that is unique among its siblings. React uses keys to tell which row is which when the array changes. Array indexes and random numbers make bad keys: state ends up beside the wrong row, or every row is rebuilt.
Conditions are plain JavaScript too: an early return null, cond ? a : b, or cond && a. React renders nothing for false, null and undefined, but it does render the number 0. So the classic pitfall is count && <Badge />, which prints a stray 0 for an empty list. Make the left side a real boolean: count > 0 && <Badge />.
Unit 02 · 3 lessons
State
Give components memory, predict what a handler sees and what the next render sees, and decide which component owns each piece of state.
- 01State
useState, renders as snapshots, updater functions and batching.
- 02Events and inputs
Handlers, controlled inputs, and updating objects and arrays without mutating them.
- BOSSWhere state lives
A filterable list end to end: lift state to the common parent, keep one source of truth, derive the rest.
Read the unit guide
Props come from outside; state is a component's own memory. const [count, setCount] = useState(0) returns the current value and a setter. Calling the setter stores a new value and asks React to call your component again. A plain variable cannot do this: changing it triggers nothing, and the next call of the function starts it from scratch.
A render is a snapshot. Each call of the component gets its own constants, and the JSX, the handlers and any timers created in that call all see those values for good. A setter never changes the variable you already have; it queues a value for the next render. So three setCount(count + 1) calls in one handler all compute the same number, and a log right after a setter prints the old value.
React waits until the handler has finished, then processes the queue in one render (batching). A plain value replaces whatever is pending. An updater function, setCount(c => c + 1), receives the pending value and builds on it, which is the form to use whenever the next state depends on the previous one.
Events connect the user to state: pass a function to onClick or onChange (pass it, do not call it). A controlled input closes the loop: value={text} shows the state, and onChange copies each edit back into it. React decides whether state changed by identity, so never mutate an object or array in state: build a new one with spread, map or filter, and set that. Spread copies one level only.
When two components need the same value, lift the state to their closest common parent: the value goes down as a prop, and a function goes down to request changes. Each piece of state has one owner. And store the minimum: anything you can compute from props or other state during render (a filtered list, a count, a full name) is a plain constant, never state. The classic pitfall is a stored copy that one handler forgets to update.
Unit 03 · 3 lessons
Rendering and effects
Predict which components re-render and whether their state survives, keep rendering pure, and use effects (with cleanup) to reach outside React.
- 01Render and commit
What triggers a render, why rendering must be pure, and when state is kept or reset.
- 02Effects
Synchronising with the world outside React: dependencies, cleanup, stale closures, and effects you do not need.
- BOSSFetch data
One request end to end: loading, error and success, the race between two responses, and refs.
Read the unit guide
A screen update has three steps. Trigger: the first render, or a setter was called. Render: React calls the component whose state changed and, by default, every component nested inside it. Commit: React compares the new output with the last one and changes only the DOM nodes that differ. Rendering means calling your functions; it is the browser that paints.
Because React may call components at any time, in any order and more than once, rendering must be pure: the same props and state give the same JSX, and nothing that existed before the call is changed. In development, Strict Mode calls each component function an extra time to expose impure ones. Changes to the outside world belong in event handlers, or in effects.
React matches the new tree to the old one by position and component type. The same component at the same position keeps its state, whatever its props. A different component at that position, a component that was removed, or the same component with a different key starts from scratch. This is why a key can reset a form, and why components are defined at the top level, never inside another component.
An effect synchronises a component with something outside React: a connection, a timer, a request. useEffect(setup, deps) runs setup after the commit; the function setup returns is the cleanup. With no array it runs after every commit, with [] after the first only, with [a, b] again whenever a or b differs (compared like ===). Before each re-run and on removal, React runs the cleanup of the previous run. Strict Mode adds one extra setup and cleanup in development to prove the cleanup works.
An effect sees the props and state of the render that created it. If it reads a value that is not in the array, it keeps the old one: the stale closure. List every value the effect reads, or stop reading it (an updater function needs no count). And do not reach for an effect to compute something from state or to react to a click: derive it during render, or do it in the handler.
Fetching puts it all together. Model the request as one state with three variants (loading, error, ok). Responses arrive in any order, so an effect that fetches needs a cleanup that makes the old response harmless: let ignore = false, check it before setting state, set it in the cleanup. Values a component must remember but never shows, such as a timer id, go in a ref: it survives renders, and changing it renders nothing.
Unit 04 · 3 lessons
Growing an app
Reuse logic with custom hooks, share state with context and reducers, and structure an app so that it stays fast, renders on a server and can be tested.
- 01Custom hooks
Why hooks have rules, how to extract your own, and when refs, useMemo and useCallback help.
- 02Context and reducers
Skip prop drilling with context, and keep state with rules in a reducer.
- BOSSStructure an app
A small shop end to end: component boundaries, a home for each state, fewer wasted renders, the server, and tests.
Read the unit guide
React stores a component's hook state in a list and hands it out in call order: first call, first slot. That is why hooks have rules. Call them only at the top level of a component (never in a condition, a loop, a nested function or after an early return), and only from components or other hooks. A custom hook is a function named use-something that calls other hooks; it shares logic, not data, so every component that calls it gets its own state.
Three hooks keep things between renders without causing renders. useRef holds a value React does not watch, or a DOM node when passed to a ref attribute. useMemo(fn, deps) reuses the last result of a calculation until a dependency changes, and useCallback(fn, deps) does the same for a function. They matter in a few cases only: a calculation that is measurably slow, or a value handed to a memoised component or to another hook's dependencies.
Context removes prop drilling: createContext(default) makes a channel, a provider sets its value for a subtree (from React 19 the context itself is the provider tag; older code writes .Provider), and useContext reads the nearest one above. When the value changes, every reader re-renders, so do not hand it a fresh object on every render. Use context for what many distant components read, and props for everything else.
A reducer gathers the rules for a piece of state in one pure function: (state, action) => next state. Components dispatch actions that say what happened; the reducer decides what changes, and returns a new value without mutating the old. Reducers can be tested by calling them, with no rendering at all.
To structure an app: give each component one job; find the minimal state; put each piece in the lowest component that covers everyone who needs it; derive the rest. State re-renders everything below its owner, so state that sits too high is the usual cause of wasted renders. Fix structure first, then measure, then reach for memo, which skips a component whose props are all identical to last time.
On a server, React can turn the tree into HTML so the user sees content before the JavaScript arrives. In the browser React then hydrates: it renders again and attaches handlers to the existing HTML, which requires the same output on both sides. Server Components go further and never reach the browser as code, so they cannot hold state. Whatever the setup, test what the user sees and does, not the names of your state variables.
Learn React on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.