Skip to content

@astrojs/ preact

Astro 整合 为你的 Preact 组件启用渲染和客户端水合。

¥This Astro integration enables rendering and client-side hydration for your Preact components.

¥Why Preact?

Preact 是一个库,可让你为 Web 构建交互式 UI 组件。如果你想使用 JavaScript 在网站上构建交互功能,你可能更喜欢使用其组件格式,而不是直接使用浏览器 API。

¥Preact is a library that lets you build interactive UI components for the web. If you want to build interactive features on your site using JavaScript, you may prefer using its component format instead of using browser APIs directly.

如果你以前使用过 React,Preact 也是一个不错的选择。Preact 提供与 React 相同的 API,但包大小小得多,只有 3kB。它甚至支持使用 compat 配置选项渲染许多 React 组件(见下文)。

¥Preact is also a great choice if you have previously used React. Preact provides the same API as React, but in a much smaller 3kB package. It even supports rendering many React components using the compat configuration option (see below).

在使用此集成之前想要了解有关 Preact 的更多信息吗?查看 “学习 Preact”,这是他们网站上的交互式教程。

¥Want to learn more about Preact before using this integration?\ Check out “Learn Preact”, an interactive tutorial on their website.

¥Installation

Astro 包含一个 astro add 命令来自动设置官方集成。如果你愿意,你可以改用 安装集成手动

¥Astro includes an astro add command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.

要安装 @astrojs/preact,请从项目目录运行以下命令并按照提示操作:

¥To install @astrojs/preact, run the following from your project directory and follow the prompts:

终端窗口
npx astro add preact

如果你遇到任何问题,请 请随时在 GitHub 上向我们报告 并尝试下面的手动安装步骤。

¥If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

¥Manual Install

首先,安装 @astrojs/preact 包:

¥First, install the @astrojs/preact package:

终端窗口
npm install @astrojs/preact

大多数包管理器也会安装相关的对等依赖。如果你在启动 Astro 时看到 “找不到包 ‘preact’“(或类似)警告,则需要安装 Preact:

¥Most package managers will install associated peer dependencies as well. 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

然后,使用 integrations 属性将集成应用于你的 astro.config.* 文件:

¥Then, apply the integration to your astro.config.* file using the integrations property:

astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
export default defineConfig({
// ...
integrations: [preact()],
});

¥Usage

要在 Astro 中使用你的第一个 Preact 组件,请前往我们的 UI 框架文档。你将探索:

¥To use your first Preact component in Astro, head to our UI framework documentation. You’ll explore:

  • 📦 如何加载框架组件,

  • 💧 客户端水合选项,以及

  • 🤝 将框架混合和嵌套在一起的机会

另请查看我们的 Astro 集成文档,了解有关集成的更多信息。

¥Also check our Astro Integration Documentation for more on integrations.

¥Configuration

Astro Preact 集成处理 Preact 组件的渲染方式,并且有自己的选项。在 astro.config.mjs 文件中更改这些内容,该文件是项目集成设置所在的位置。

¥The Astro Preact integration handles how Preact components are rendered and it has its own options. Change these in the astro.config.mjs file which is where your project’s integration settings live.

对于基本使用,你不需要配置 Preact 集成。

¥For basic usage, you do not need to configure the Preact integration.

你可以启用 preact/compat(Preact 的兼容层)来渲染 React 组件,而无需将 React 的较大库安装或发送到用户的 Web 浏览器。

¥You can enable preact/compat, Preact’s compatibility layer for rendering React components without needing to install or ship React’s larger libraries to your users’ web browsers.

为此,请将对象传递给 Preact 集成并设置 compat: true

¥To do so, pass an object to the Preact integration and set compat: true.

astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
export default defineConfig({
integrations: [preact({ compat: true })],
});

启用 compat 选项后,Preact 集成将在项目中渲染 React 组件和 Preact 组件,并允许你在 Preact 组件中导入 React 组件。在 Preact 网站上阅读 “(从 React)切换到 Preact” 的更多内容。

¥With the compat option enabled, the Preact integration will render React components as well as Preact components in your project and also allow you to import React components inside Preact components. Read more in “Switching to Preact (from React)” on the Preact website.

导入 React 组件库时,为了将 reactreact-dom 依赖替换为 preact/compat,可以使用 overrides 来完成。

¥When importing React component libraries, in order to swap out the react and react-dom dependencies as preact/compat, you can use overrides to do so.

package.json
{
"overrides": {
"react": "npm:@preact/compat@latest",
"react-dom": "npm:@preact/compat@latest"
}
}

查看 pnpm 覆盖yarn 号决议 文档以了解它们各自的覆盖功能。

¥Check out the pnpm overrides and yarn resolutions docs for their respective overrides features.

Added in: @astrojs/preact@3.3.0

你可以通过将带有 devtools: true 的对象传递给你的 preact() 集成配置来在开发中启用 Preact devtools

¥You can enable Preact devtools in development by passing an object with devtools: true to your preact() integration config:

astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
export default defineConfig({
// ...
integrations: [preact({ devtools: true })],
});

¥Options

¥Combining multiple JSX frameworks

当你在同一项目中使用多个 JSX 框架(React、Preact、Solid)时,Astro 需要确定每个组件应使用哪些特定于 JSX 框架的转换。如果你只在项目中添加了一个 JSX 框架集成,则不需要额外的配置。

¥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.

使用 include(必需)和 exclude(可选)配置选项来指定哪些文件属于哪个框架。为你正在使用的每个框架向 include 提供一系列文件和/或文件夹。通配符可用于包含多个文件路径。

¥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.

我们建议将通用框架组件放在同一文件夹中(例如 /components/react//components/solid/),以便更轻松地指定包含内容,但这不是必需的:

¥We 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:

astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
import react from '@astrojs/react';
import svelte from '@astrojs/svelte';
import vue from '@astrojs/vue';
import solid from '@astrojs/solid-js';
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/*'],
}),
solid({
include: ['**/solid/*'],
}),
],
});

¥Examples

  • Astro Preact 示例 展示了如何在 Astro 项目中使用交互式 Preact 组件。

  • Astro Nanostores 示例 展示了如何在不同组件之间共享状态 - 甚至不同的框架!— 在 Astro 项目中。

更多集成

前端框架

SSR 适配器

其他集成

Astro 中文网 - 粤ICP备13048890号