{
  "url": "https://unifast.dev/zh-CN/docs/packages/react/hast-to-react/",
  "locale": "zh-CN",
  "title": "hastToReact()",
  "description": "将 HAST 根节点转换为 React 元素。当你需要对编译管线进行完全控制时使用的低层函数。",
  "section": "packages",
  "body": "```ts\n\n```\n\n## 签名\n\n```ts\nfunction hastToReact(\n  hast: HastRoot,\n  options: HastToReactOptions,\n): unknown\n```\n\n## 参数\n\n### hast\n\n| 属性 | 类型 | 默认值 | 描述 |\n|----------|------|---------|-------------|\n| `type` | `\"root\"` | — | 节点类型标识 |\n| `children` | `HastNode[]` | — | 树的子节点 |\n\n### options\n\nReact 元素创建的配置\n\n| 属性 | 类型 | 默认值 | 描述 |\n|----------|------|---------|-------------|\n| `createElement` | `CreateElement` | — | React 的 createElement 函数 |\n| `Fragment` | `unknown` | — | React 的 Fragment 组件 |\n| `components?` | `ComponentMap` | — | HTML 标签名到自定义 React 组件的映射 |\n\n## 用法\n\n```tsx\n\nconst result = compile(md, { outputKind: \"hast\" });\nconst hast: HastRoot = JSON.parse(result.output as string);\n\nconst element = hastToReact(hast, {\n  createElement,\n  Fragment,\n  components: {\n    pre: ({ children, ...props }) => <pre className=\"code-block\" {...props}>{children}</pre>,\n    a: ({ children, ...props }) => <a target=\"_blank\" rel=\"noopener\" {...props}>{children}</a>,\n  },\n});\n```\n\n## 示例\n\n### 基本用法\n\n```tsx\n\nconst result = compile(\"# Hello, **world**!\", { outputKind: \"hast\" });\nconst hast: HastRoot = JSON.parse(result.output as string);\nconst element = hastToReact(hast, { createElement, Fragment });\n\nfunction Page() {\n  return <div>{element}</div>;\n}\n```\n\n### 处理经过 Shiki 高亮的 HAST\n\n```tsx\n\nconst transformer = await createShikiTransformer({\n  themes: [\"github-dark\"],\n  langs: [\"typescript\"],\n});\n\nconst result = compile(md, { outputKind: \"hast\" });\nconst hast: HastRoot = JSON.parse(result.output as string);\nconst highlighted = transformer.transform(hast);\nconst element = hastToReact(highlighted, { createElement, Fragment });\n```\n\n### 服务端渲染\n\n```tsx\n\nconst result = compile(md, { outputKind: \"hast\" });\nconst hast: HastRoot = JSON.parse(result.output as string);\nconst element = hastToReact(hast, { createElement, Fragment });\nconst html = renderToString(element);\n\nconsole.log(html);\n// Rendered HTML string for SSR\n```\n\n## 行为说明\n\n- **属性重命名：** HTML 属性会被重命名为 React 对应的写法（`class` → `className`、`for` → `htmlFor` 等）\n- **样式解析：** CSS 样式字符串会被解析为 React 的样式对象\n- **className 数组：** HAST 中的 `className` 数组会用空格拼接\n- **布尔属性：** `true` 会输出属性本身，`false`/`null`/`undefined` 会被省略\n- **Raw 节点：** 会作为内联 HTML 渲染 —— 处理不受信任的输入时，请使用（来自 `@unifast/node` 的）`sanitize` 插件\n- **注释和 doctype：** 会被忽略（返回 `null`）",
  "alternates": [
    {
      "locale": "en",
      "url": "https://unifast.dev/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "ja",
      "url": "https://unifast.dev/ja/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/ja/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "zh-CN",
      "url": "https://unifast.dev/zh-CN/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/zh-CN/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "zh-TW",
      "url": "https://unifast.dev/zh-TW/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/zh-TW/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "ko",
      "url": "https://unifast.dev/ko/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/ko/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "fr",
      "url": "https://unifast.dev/fr/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/fr/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "it",
      "url": "https://unifast.dev/it/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/it/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "es",
      "url": "https://unifast.dev/es/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/es/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "pt-BR",
      "url": "https://unifast.dev/pt-BR/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/pt-BR/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "de",
      "url": "https://unifast.dev/de/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/de/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "ru",
      "url": "https://unifast.dev/ru/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/ru/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "hi",
      "url": "https://unifast.dev/hi/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/hi/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "id",
      "url": "https://unifast.dev/id/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/id/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "tr",
      "url": "https://unifast.dev/tr/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/tr/docs/packages/react/hast-to-react.json"
    },
    {
      "locale": "vi",
      "url": "https://unifast.dev/vi/docs/packages/react/hast-to-react/",
      "api": "https://unifast.dev//api/vi/docs/packages/react/hast-to-react.json"
    }
  ]
}
