side project
Leer en españolreact-learn
A small React you can watch think: a commented re-implementation of React’s core (fibers, lanes, hooks, Suspense) with DevTools that show every internal step.
What it is
I built react-learn to see how React works from the inside. It is a small, commented re-implementation of React’s core in TypeScript, covering fibers, lanes, hooks, concurrent rendering and Suspense, written to be read rather than to ship.
A course of 25 planned episodes builds it up step by step, and each example links to the matching React docs. Some things are left out on purpose: class components, error boundaries, hydration, portals and server components.
How it's built
I built react-learn to see how React works from the inside. It is a small, heavily commented re-implementation of React’s core in TypeScript: fibers, lanes, hooks, concurrent rendering, Suspense and some React 19 features. DevTools hook into every internal step, so you can watch a render happen and step through it.
highlights
A library written to be read
Every source file starts with a long comment, and the entry file lists the reading order: elements, fibers, lanes, hooks and the update queue, the work loop, begin and complete work, commit, then the scheduler. It covers double-buffered fibers, a keyed array diff, a mini scheduler with 5 ms slices and a commit with mutation, layout and passive phases, in about 5,300 lines with the comments.
Lanes as bits, 11 instead of 31
A lane is one bit, and lower bits are higher priority, so checking whether a render includes an update, merging priorities or finding the most urgent work is one bit operation. React has 31 lanes; I kept 11, enough to show every idea, and the comment at the top of the lanes file explains which event produces each one.
Update queues with rebasing
When a render skips a low-priority update, the queue keeps a base state and a base queue, so the skipped update and the ones after it are replayed later on top. The update-queue file walks through that case step by step, a test covers it, and the DevTools show what happened to each update: applied, skipped, kept for rebase, or optimistically applied and reverted.
Suspense that keeps state
A Suspense boundary always wraps its children in an Offscreen fiber. To show the fallback, the Offscreen switches to hidden, so its fibers and DOM stay and state survives. use(promise) throws, the work loop catches it and a retry lane renders again when the promise resolves; for transitions it keeps the old UI instead. The same Offscreen idea powers Activity.
DevTools made of events, rendered by the real React
The library emits an event at each step and does nothing if nobody listens, like React’s own DevTools hook but more verbose. The DevTools store snapshots both fiber trees on each event with structural sharing, so time travel only allocates what changed, and a debug mode can pause the work loop to advance one fiber per click. The DevTools UI is rendered with the real react-dom, so its own renders never show up in the timeline.
A course of 25 planned episodes
The course plan splits the library into short lessons (one concept, one file, its own small demo), a few merges, and milestones that are runnable Reacts, from first paint to Suspense and React 19. Lessons use the same names as the final source, so merging is copy and paste, and only the finale shows everything with the DevTools.
- TypeScript
- React 19 (DevTools)
- Storybook
- Vite
- Vitest
- jsdom