Skip to content
Migrate from Preact · @pyreon/preact-compat

Preact code, hooks and signals included.

@pyreon/preact-compat mirrors Preact's module layout (the root, /hooks and /signals), so imports from preact, preact/hooks and @preact/signals resolve to it with no edits. Hooks keep Preact's value-and-re-render model; signals keep .value.

npm install @pyreon/preact-compat @pyreon/core @pyreon/reactivity @pyreon/runtime-dom npm install -D @pyreon/vite-plugin
before and after

The same Preact code, on Pyreon.

Migrating from Preact: before and after Preact to Pyreon, before and after: the same Preact component (11 lines) and its Pyreon version (11 lines). 6 lines change; the changed lines are marked with a minus rail in the before code and a plus rail in the after code. Code before: import { useState } from 'preact/hooks' export function Counter({ step }: { step: number }) { const [count, setCount] = useState(0) const doubled = count * 2 return ( <button class="counter" onClick={() => setCount(count + step)}> {count} / {doubled} </button> ) } Code after: import { computed, signal } from '@pyreon/reactivity' export function Counter(props: { step: number }) { const count = signal(0) const doubled = computed(() => count() * 2) return ( <button class="counter" onClick={() => count.update((c) => c + props.step)}> {count()} / {doubled()} </button> ) } MIGRATE FROM PREACT · THEN, ONE COMPONENT AT A TIMEBEFORE · PREACT (RUNS AS-IS ON PREACT-COMPAT)−import { useState } from 'preact/hooks'−export function Counter({ step }: { step: number }) {− const [count, setCount] = useState(0)− const doubled = count * 2 return (− <button class="counter" onClick={() => setCount(count + step)}>− {count} / {doubled} </button> )}AFTER · PYREON+import { computed, signal } from '@pyreon/reactivity'+export function Counter(props: { step: number }) {+ const count = signal(0)+ const doubled = computed(() => count() * 2) return (+ <button class="counter" onClick={() => count.update((c) => c + props.step)}>+ {count()} / {doubled()} </button> )}
An illustration of the code on each side, not a recording.
coverage

What @pyreon/preact-compat covers, and what it does not.

covers
  • Core: h / createElement, Fragment, render, hydrate, Component, PureComponent, createContext, createRef, cloneElement, toChildArray, isValidElement, createPortal, lazy, Suspense, ErrorBoundary, options.
  • /hooks: useState, useReducer, useEffect, useLayoutEffect, useMemo, useCallback, useRef, useId, memo, forwardRef, useImperativeHandle, useDebugValue, useTransition, useDeferredValue, useErrorBoundary.
  • /signals: signal, computed, effect, batch, with the .value getter and setter.
  • Class lifecycle: componentDidMount, componentDidUpdate, componentWillUnmount and shouldComponentUpdate fire.
does not cover
  • componentDidCatch, getDerivedStateFromError, getDerivedStateFromProps, getSnapshotBeforeUpdate: class-based error boundaries do not catch. Use the ErrorBoundary component.
  • Nested state across a parent re-render: the parent rebuilds its subtree, so a child's useState, useReducer and useRef reset and useEffect([]) runs again. memo does not stop it; lift the state up.
  • useLayoutEffect runs with useEffect's timing.
  • The plugin aliases preact, preact/hooks, preact/jsx-runtime and @preact/signals; preact/compat and @preact/signals-core are not aliased.
  • version reports 10.0.0-pyreon.
  • The component hooks of @preact/signals (useSignal, useComputed, useSignalEffect) are not exported, so an import of them fails.
setup

Install it, point the config at it.

npm install @pyreon/preact-compat @pyreon/core @pyreon/reactivity @pyreon/runtime-dom npm install -D @pyreon/vite-plugin
vite.config.ts
// vite.config.ts
import { defineConfig } from 'vite'
import pyreon from '@pyreon/vite-plugin'

export default defineConfig({
  plugins: [pyreon({ compat: 'preact' })],
})

Use it in place of @preact/preset-vite. Aliases preact, preact/hooks and @preact/signals.

tsconfig.json
// tsconfig.json
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "@pyreon/preact-compat"
  }
}

Types and the JSX runtime come from the compat package.

or start from a scaffold
npm create @pyreon/zero@latest my-app -- --compat preact
port it

Then, one component at a time

Preact (runs as-is on preact-compat) · Counter.tsx
import { useState } from 'preact/hooks'

export function Counter({ step }: { step: number }) {
  const [count, setCount] = useState(0)
  const doubled = count * 2
  return (
    <button class="counter" onClick={() => setCount(count + step)}>
      {count} / {doubled}
    </button>
  )
}
Pyreon · Counter.tsx
import { computed, signal } from '@pyreon/reactivity'

export function Counter(props: { step: number }) {
  const count = signal(0)
  const doubled = computed(() => count() * 2)
  return (
    <button class="counter" onClick={() => count.update((c) => c + props.step)}>
      {count()} / {doubled()}
    </button>
  )
}

The Pyreon side is the final form, after compat is switched off. While the app is still in compat mode, wrap it in nativeCompat() and write the text as an arrow.