To use Preact in Astro, run npx astro add preact, write components in .jsx or .tsx files, and render them in .astro pages with a client directive such as client:visible. Astro Preact islands use the same JSX you’d write for React, with a much smaller runtime, and Preact signals can be passed as props to share reactive state between islands. Turn on compat: true if you need React libraries.
Key Takeaways
- Preact gives you React-style components with a far smaller runtime, which suits small islands.
npx astro add preactinstalls everything and configures JSX.- Signals from
@preact/signalscan be passed as island props and stay reactive.preact({ compat: true })lets many React libraries run on Preact.
Install
# terminal
npx astro add preact
This installs @astrojs/preact and preact, adds the integration, and sets the JSX options in tsconfig.json:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
export default defineConfig({
integrations: [preact()],
});
A first island
// src/components/Counter.tsx
import { useState } from 'preact/hooks';
export default function Counter({ start = 0 }: { start?: number }) {
const [count, setCount] = useState(start);
return (
<button type="button" onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}
---
// src/pages/index.astro
import Counter from '../components/Counter.tsx';
---
<Counter client:visible start={3} />
Without a directive, the component renders to static HTML and ships no JavaScript. With one, it hydrates in the browser. Hooks come from preact/hooks, not react.
Signals across islands
Signals are Preact’s reactive values. Define one in a module and import it in several islands, or pass it as a prop from the page and Astro keeps it reactive:
// src/state/filters.ts
import { signal } from '@preact/signals';
export const query = signal('');
// src/components/SearchBox.tsx
import { query } from '../state/filters';
export default function SearchBox() {
return <input value={query} onInput={(e) => (query.value = e.currentTarget.value)} placeholder="Filter" />;
}
// src/components/ResultCount.tsx
import { query } from '../state/filters';
export default function ResultCount({ total }: { total: number }) {
return <p>{query.value ? `Filtering by “${query.value}”` : `${total} items`}</p>;
}
Install @preact/signals first. Signals only share state between Preact islands; to include React, Svelte or Vue islands, use Nano Stores.
React libraries with compat
Preact’s compatibility layer maps react and react-dom imports to Preact, so many React packages work unchanged:
// astro.config.mjs
import preact from '@astrojs/preact';
export default defineConfig({
integrations: [preact({ compat: true })],
});
Compat covers most component libraries and hooks-based packages. Libraries that depend on React internals or the newest React-only APIs may not work, so test the ones you need.
Preact and React in one project
If you also use the React integration, both treat .jsx and .tsx files as their own. Tell each which files belong to it with include:
// astro.config.mjs
import preact from '@astrojs/preact';
import react from '@astrojs/react';
export default defineConfig({
integrations: [
preact({ include: ['**/preact/*'] }),
react({ include: ['**/react/*'] }),
],
});
Then keep components in src/components/preact/ and src/components/react/.
Choose Preact or React
| Preact | React | |
|---|---|---|
| Runtime size | Very small | Larger |
| API | Same JSX and hooks (from preact/hooks) |
The reference implementation |
| Ecosystem | Most React libraries via compat | Everything |
| Best for | Small islands on content sites | Apps relying on React-only libraries |
On a content site with a few small islands, Preact is usually the better default. Our comparison of React, Svelte and Vue islands covers the wider choice, and React components in Astro covers the React route. The config docs show where integrations go in a Komal theme, and you can add Preact to any of our Astro themes with the command above.
FAQ
Is Preact faster than React in Astro?
Preact’s runtime is much smaller, so islands download and start faster, especially on slow phones. Rendering speed for small components is similar; the difference is mostly in JavaScript size.
Can I use React libraries with Preact in Astro?
Many work with preact({ compat: true }), which aliases react imports to Preact. Test libraries that use React internals or React-only features.
Can I pass Preact signals as props in Astro?
Yes. Astro serializes signals passed to Preact islands and keeps them reactive on the client, so islands that receive the same signal stay in sync.