Skip to content

How to understand a Preact codebase

Preact is a small library with React's component API. Many Preact apps use React libraries through `preact/compat`, which the build aliases in place of `react`. Some use `@preact/signals` for state, which updates the DOM without re-rendering whole components.

How to tell it's Preact

  • preact in package.json, often with @preact/preset-vite
  • Imports from preact, preact/hooks or preact/compat
  • Aliases mapping react and react-dom to preact/compat in the bundler config

Where things usually live

src/index.tsx or main.tsx
Calls render(<App />, element).
src/components/
Function components using hooks from preact/hooks.
src/state/ or signals.ts
Signals created with signal() and computed().
vite.config.ts
The Preact preset and any react aliases.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The render call, to see the root component
  2. The bundler config, to check whether React libraries are aliased
  3. Any signals module, for shared state

How a request moves through the code

  1. 1render mounts the root component.
  2. 2Components use hooks for local state, or read signals for shared state.
  3. 3Reading signal.value inside JSX subscribes that spot; updating it patches the DOM directly.
  4. 4React libraries run through preact/compat.

Questions to ask about the repository

  • Which React libraries does this app depend on through preact/compat?
  • Where are signals defined, and which components read them?
  • Is any code relying on React-only behaviour that compat may not cover?

What trips people up

  • Some React libraries depend on React internals and break subtly under compat.
  • Preact passes DOM attributes more directly than React, so class and className may both appear.