Skip to content

@astrojs/ react

Astro 整合 为你的 React 组件启用服务器端渲染和客户端水合作用。

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

¥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/react,请从项目目录运行以下命令并按照提示操作:

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

Terminal window
npx astro add react

如果你遇到任何问题,请 请随时在 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/react 包:

¥First, install the @astrojs/react package:

Terminal window
npm install @astrojs/react

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

¥Most package managers will install associated peer dependencies as well. If you see a “Cannot find package ‘react’” (or similar) warning when you start up Astro, you’ll need to install react and react-dom:

Terminal window
npm install react react-dom

然后,使用 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 react from '@astrojs/react';
export default defineConfig({
// ...
integrations: [react()],
});

¥Getting started

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

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

  • 📦 如何加载框架组件,

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

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

¥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/*'],
}),
],
});

¥Children parsing

从 Astro 组件传递到 React 组件的子项被解析为纯字符串,而不是 React 节点。

¥Children passed into a React component from an Astro component are parsed as plain strings, not React nodes.

例如,下面的 <ReactComponent /> 将仅接收一个子元素:

¥For example, the <ReactComponent /> below will only receive a single child element:

---
import ReactComponent from './ReactComponent';
---
<ReactComponent>
<div>one</div>
<div>two</div>
</ReactComponent>

如果你使用的库期望传递多个子元素,例如,以便它可以将某些元素放置在不同的位置,你可能会发现这是一个阻止程序。

¥If you are using a library that expects more than one child element to be passed, for example so that it can slot certain elements in different places, you might find this to be a blocker.

你可以设置实验标志 experimentalReactChildren 以告诉 Astro 始终将子节点作为 React 虚拟 DOM 节点传递给 React。这会产生一些运行时成本,但它有助于提高兼容性。

¥You can set the experimental flag experimentalReactChildren to tell Astro to always pass children to React as React virtual DOM nodes. There is some runtime cost to this, but it can help with compatibility.

你可以在 React 集成的配置中启用此选项:

¥You can enable this option in the configuration for the React integration:

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

More integrations

UI Frameworks

SSR Adapters

Other integrations

Astro 中文网 - 粤ICP备13048890号