@astrojs/ react
此 Astro 整合 为你的 React 组件启用渲染和客户端水合。
¥This Astro integration enables 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:
如果你遇到任何问题,请 请随时在 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:
大多数包管理器也会安装相关的对等依赖。如果你在启动 Astro 时看到 “找不到包 ‘react’“(或类似)警告,则需要安装 react
和 react-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
:
然后,使用 integrations
属性将集成应用于你的 astro.config.*
文件:
¥Then, apply the integration to your astro.config.*
file using the integrations
property:
入门
标题部分 入门¥Getting started
要在 Astro 中使用你的第一个 React 组件,请前往我们的 UI 框架文档。你将探索:
¥To use your first React component in Astro, head to our UI framework documentation. You’ll explore:
-
📦 如何加载框架组件,
-
💧 客户端水合选项,以及
-
🤝 将框架混合和嵌套在一起的机会
选项
标题部分 选项¥Options
组合多个 JSX 框架
标题部分 组合多个 JSX 框架¥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:
子级们解析
标题部分 子级们解析¥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:
如果你使用的库期望传递多个子元素,例如,以便它可以将某些元素放置在不同的位置,你可能会发现这是一个阻止程序。
¥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: