Installation
Quick Install
The astro add
command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren’t sure which package manager you’re using, run the first command.) Then, follow the prompts, and type “y” in the terminal (meaning “yes”) for each one.
npx astro add preact
yarn astro add preact
pnpm astro add preact
Manual Install
First, install the @astrojs/preact
package using your package manager. If you’re using npm or aren’t sure, run this in the terminal:
npm install @astrojs/preact
Most package managers will install associated peer dependencies as well. Still, if you see a “Cannot find package ‘preact’” (or similar) warning when you start up Astro, you’ll need to install Preact:
npm install preact
Then, apply this integration to your astro.config.* file using the integrations property:
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
export default defineConfig({
// ...
integrations: [preact()],
// ^^^^^^^^
});
Usage
To use your first Preact component in Astro, refer to the UI framework documentation. You’ll explore:
Options
Combining multiple JSX frameworks
When you are using multiple JSX frameworks (React, Preact, Solid) in the same project, Astro needs to determine which JSX framework-specific transformations should be used for each of your components. If you have only added one JSX framework integration to your project, no extra configuration is needed.
Use the include (required) and exclude (optional) configuration options to specify which files belong to which framework. Provide an array of files and/or folders to include for each framework you are using. Wildcards may be used to include multiple file paths.
I recommend placing common framework components in the same folder (e.g. /components/react/ and /components/solid/) to make specifying your includes easier, but this is not required:
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
import react from '@astrojs/react';
export default defineConfig({
// Enable many frameworks to support all different kinds of components.
// No `include` is needed if you are only using a single JSX framework!
integrations: [
preact({
include: ['**/preact/*'],
}),
react({
include: ['**/react/*'],
})
],
});
tsconfig.json
{
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact", // or react depending upon the integration
},
"exclude": ["dist"] // to exclude type checking in build files
}
Common Errors
Sometimes Astro is not able to identify which framework is used if you don't have any import statement from that particular framework in your framework component. Due to this, the astro check
command may fail to resolve the issue. In such cases, you can use a per-file pragma to set the correct jsxImportSource:
/** @jsxImportSource preact */
Top comments (2)
Helpfull...
i was struggling with this for a long time.
it solved my problem
Thanks
I'm delighted to be of assistance.