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.
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.valuegetter and setter.- Class lifecycle:
componentDidMount,componentDidUpdate,componentWillUnmountandshouldComponentUpdatefire.
does not cover
componentDidCatch,getDerivedStateFromError,getDerivedStateFromProps,getSnapshotBeforeUpdate: class-based error boundaries do not catch. Use theErrorBoundarycomponent.- Nested state across a parent re-render: the parent rebuilds its subtree, so a child's
useState,useReduceranduseRefreset anduseEffect([])runs again.memodoes not stop it; lift the state up. useLayoutEffectruns withuseEffect's timing.- The plugin aliases
preact,preact/hooks,preact/jsx-runtimeand@preact/signals;preact/compatand@preact/signals-coreare not aliased. versionreports10.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.
read more