在 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 PdfsUIzhTW from '@univerjs-pro/pdfs-ui/locale/zh-TW'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { FUniver } from '@univerjs/core/facade'import DesignzhTW from '@univerjs/design/locale/zh-TW'import { UniverUIPlugin } from '@univerjs/ui'import UIzhTW from '@univerjs/ui/locale/zh-TW'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_TW,      locales: {        [LocaleType.ZH_TW]: mergeLocales(          DesignzhTW,          UIzhTW,          PdfsUIzhTW,        ),      },    })    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.