How it works · small JavaScript library for building screens

How Preact works

The same idea as React, in a few kilobytes: you describe the screen, and Preact changes only what is different.

Think of it like this. A decorator with a list of how the room should look. They walk the room once, comparing each thing to the list, and fix what doesn’t match as they go.

  1. Descriptions Every tag you write becomes a small object that says what should be there. src/create-element.js
  2. Render The way in: put this description into that place on the page. src/render.js
  3. The diff Compares the new description with the last one and changes the page where they differ. src/diff
  4. Components Remembers which components asked to be drawn again, and redraws them together. src/component.js
  5. Hooks useState, useEffect and the rest, kept in a separate small package. hooks/src/index.js

Follow one thing through: you press a button that calls setCount(1)

  1. The hook finds its own slot in the component’s list of hooks, by the order it was called in.hooks/src/index.js
  2. The component is marked as needing a redraw and added to a queue. Several changes in a row make one redraw.src/component.js
  3. A moment later the queue is worked through, parents before children.src/component.js
  4. Your component function runs again and returns a new description.src/diff/index.js
  5. Its children are matched with the old ones, by key and type, so the same thing keeps the same element.src/diff/children.js
  6. Only the attributes and text that changed are written to the page.src/diff/props.js

The clever bit: One listener per event, however often you re-render

Your onClick is usually a new function on every render. Adding and removing a browser listener each time would be slow. So Preact attaches one small function per event name to the element, and keeps your handler in a table on the element itself (dom._listeners). A re-render just swaps the entry in the table. The browser’s listener never changes.

src/diff/props.js

Words you’ll meet

virtual node
A small object that describes one tag or component.
diff
Comparing the new description with the old one to find what changed.
key
A name you give a list item so it can be recognised when the list changes.
hook
A function like useState that gives a component memory.

Good questions to ask next

Open Preact in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.

Take Preact apart yourself.

Free and open source. Nothing to install, no account.

Ideas inside it

More projects, taken apart

How React worksYou describe what the screen should look like. React works out the smallest set of changes to make it so.How Express worksA request comes in and passes through a list of functions, in order, until one of them answers.How Flask worksYou write a function for each address. Flask finds the right one for a request, calls it, and turns what it…How Redis worksOne big dictionary held in memory, answered by a single loop that takes one command at a time.How nanoGPT worksText becomes numbers, a model learns to guess the next number, and guessing again and again writes new text.How micrograd worksEvery number remembers how it was made, so the answer can be traced back to say how much each input mattered.How SQLite worksYour SQL is compiled into a small program, and a tiny virtual machine runs it against one file on disk.How FastAPI worksYou write a function with type hints. FastAPI reads the hints to check the request, convert the values and…How Vue worksVue notices which data each part of the screen reads, so when that data changes it knows exactly what to…How Requests worksOne friendly line, requests.get(url), on top of the fiddly work of opening connections and speaking HTTP.How Redux worksAll of an app’s state lives in one place, and the only way to change it is to describe what happened.How Git worksEvery version of every file is stored under a name made from its own contents, and a commit is a small note…How Zustand worksA store is a value, a way to change it, and a list of who wants to know. The whole core is about a hundred…How Axios worksOne way to make web requests that works the same in a browser and in Node, with hooks before and after every…How htmx worksAny element can ask the server for a piece of HTML and put it into the page. You write attributes, not…How LevelDB worksA store for keys and values that makes writing fast by never changing a file: it only adds, and tidies up…How etcd’s Raft worksSeveral servers keep the same list of changes in the same order, even when some of them crash. This is the…

Written by hand from the code as it was on 2026-11-30 (commit 05d9e9b). Preact belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.