在 React 中集成

请先阅读安装和基本使用。下面的 React 组件将 Univer Pdfs 挂载到容器中,容器高度需要显式指定。

集成步骤

  1. 在 useEffect 中创建 Univer 实例。
  2. 在 useEffect cleanup 中销毁该实例。

插件配置

UniverUIPlugin

  • container
    string | HTMLElement
    挂载目标 DOM 元素或元素 ID。

UniverLicensePlugin

  • license
    string
    客户端许可证文件的内容。

示例代码

每次挂载都使用独立的子容器。实例在微任务中销毁,该微任务在外层 React 清理完成之后执行,因此嵌套的 React root 不会在 React 提交期间被同步卸载。

TSX
import { useEffect, useRef } from 'react'import { UniverLicensePlugin } from '@univerjs-pro/license'import { UniverPdfsPlugin } from '@univerjs-pro/pdfs'import { UniverPdfsUIPlugin } from '@univerjs-pro/pdfs-ui'import PdfsUIzhCN from '@univerjs-pro/pdfs-ui/locale/zh-CN'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { FUniver } from '@univerjs/core/facade'import DesignzhCN from '@univerjs/design/locale/zh-CN'import { UniverUIPlugin } from '@univerjs/ui'import UIzhCN from '@univerjs/ui/locale/zh-CN'import '@univerjs/design/lib/index.css'import '@univerjs/ui/lib/index.css'import '@univerjs-pro/pdfs-ui/lib/index.css'import '@univerjs-pro/pdfs/facade'export function UniverEditor() {  const containerRef = useRef<HTMLDivElement>(null)  useEffect(() => {    const host = containerRef.current    if (!host) return    const container = document.createElement('div')    container.style.height = '100%'    host.append(container)    const univer = new Univer({      locale: LocaleType.ZH_CN,      locales: {        [LocaleType.ZH_CN]: mergeLocales(          DesignzhCN,          UIzhCN,          PdfsUIzhCN,        ),      },    })    univer.registerPlugin(UniverLicensePlugin, {      license: 'your license.txt',    })    univer.registerPlugin(UniverUIPlugin, { container })    univer.registerPlugin(UniverPdfsPlugin)    univer.registerPlugin(UniverPdfsUIPlugin)    const univerAPI = FUniver.newAPI(univer)    univerAPI.createPdf({ name: 'Untitled PDF' })    return () => {      queueMicrotask(() => {        univer.dispose()        container.remove()      })    }  }, [])  return <div ref={containerRef} style={{ height: 600 }} />}

你觉得这篇文档如何?

© 2026 DreamNum Co., Ltd.