React에 통합하기
React 구성품을 사용자 정의 구성품으로 사용하려면 사용자 정의 구성를 참조하십시오.
Univer를 React 프로젝트에 추가하기 전에 Univer 설치 및 기본 사용을 읽고 React의 핵심 개념과 수명주기 후크와 익숙해져 있는지 확인하십시오.
React 18 및 19와 Web SDK 통합하기
통합 단계
useEffect안에서 Univer를 초기화합니다.useEffect에 의해 반환된 정리 기능에서 그것을 파괴하십시오.
프리셋 설정
UniverDocsCorePreset
containerstring | HTMLElement기본값:'app'편집기를 마운트할 DOM 요소 또는 해당 요소의 ID입니다.
예제
각 마운트는 자신의 아이 컨테이너를 만듭니다. Disposal은 React 정리 후 마이크로 작업에서 실행되므로 React 루트는 React 커밋에서 비동기적으로 배포되지 않습니다.
TSX
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'import UniverPresetDocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import { useEffect, useRef } from 'react'import '@univerjs/preset-docs-core/lib/index.css'export function App() { 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, univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetDocsCoreEnUS, ), }, presets: [ UniverDocsCorePreset({ container, }), ], }) univerAPI.createDocument({}) return () => { queueMicrotask(() => { univer.dispose() container.remove() }) } }, []) return ( <div ref={containerRef} style={{ height: 600 }} /> )}React 16.9+ 및 17과 Web SDK 통합하기
통합 전 참고 사항
호환성 참고 사항
Univer의 전망 층은 React 18.3.1에 건축됩니다. React 16.9+ & 17 프로젝트는 최소한도 겸용 지원을 받고, 그 지원은 무한하게 지속되지 않을 것입니다. 새로운 특징 및 제일 경험은 최신 React 버전에, 그래서 향상 추천됩니다.
빌드 도구 별칭 설정
React 16.9+ & 17 프로젝트에서, shim에 react-dom/client를 점하기 위하여 구조 공구의 별명으로 특징을 이용합니다. 이 별명으로 없이, 몇몇 특징은 작동하지 않습니다.
Vite 설정 예제
TypeScript
export default defineConfig({ plugins: [react()], resolve: { alias: { 'react-dom/client': path.resolve(__dirname, './src/client.ts'), }, },})TypeScript
import ReactDOM from 'react-dom'export function createRoot(container: HTMLElement) { return { render: (element: JSX.Element) => { ReactDOM.render(element, container) }, }}CDN/UMD 환경 대응
Univer 및 React가 <script> 태그를 통해로드되면 React로드 후 다음 코드를 추가합니다. UMD 빌드가 제공하지 않는 react-dom/client 수출을 공급합니다.
HTML
<script> /** * With the support for React 19[^1], UMD users may need additional adaptation. * * [^1]: [support for React 19] https://github.com/dream-num/univer/pull/4247 */ /** * Fix `Uncaught TypeError: client.createRoot is not a function` * If using UMD React < 18, you might need the following code. */ ;(function (global) { 'use strict' if (!global.ReactDOM) { throw new Error('ReactDOM must be loaded before ReactCreateRoot.') } const ReactDOM = global.ReactDOM if (!ReactDOM.createRoot) { ReactDOM.createRoot = function (container) { return { render: (element) => { ReactDOM.render(element, container) }, } } } })(this) /** * Fix `Uncaught TypeError: jsxRuntime.jsx is not a function` * If using UMD React, you might need the following code. * Reference: https://unpkg.com/react@18.3.1/cjs/react-jsx-runtime.production.min.js */ ;(function (global) { 'use strict' if (!global.React) { throw new Error('React must be loaded before ReactJSXRuntime.') } const React = global.React if (!React.jsx || !React.jsxs) { const REACT_ELEMENT_TYPE = Symbol.for('react.element') const hasOwnProperty = Object.prototype.hasOwnProperty const RESERVED_PROPS = { key: true, ref: true, __self: true, __source: true, } const ReactCurrentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner function createReactElement(type, config, maybeKey) { const props = {} let key = null let ref = null if (maybeKey !== undefined) { key = `${maybeKey}` } if (config.key !== undefined) { key = `${config.key}` } if (config.ref !== undefined) { ref = config.ref } for (var propName in config) { if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) { props[propName] = config[propName] } } if (type && type.defaultProps) { const defaultProps = type.defaultProps for (var propName in defaultProps) { if (props[propName] === undefined) { props[propName] = defaultProps[propName] } } } return { $$typeof: REACT_ELEMENT_TYPE, type, key, ref, props, _owner: ReactCurrentOwner.current, } } React.jsx = createReactElement React.jsxs = createReactElement } })(this)</script>통합 단계
useEffect안에서 Univer를 초기화합니다.useEffect에 의해 반환된 정리 기능에서 그것을 파괴하십시오.
예제
TSX
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'import UniverPresetDocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import { useEffect, useRef } from 'react'import '@univerjs/preset-docs-core/lib/index.css'export function App() { 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, univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetDocsCoreEnUS, ), }, presets: [ UniverDocsCorePreset({ container, }), ], }) univerAPI.createDocument({}) return () => { queueMicrotask(() => { univer.dispose() container.remove() }) } }, []) return ( <div ref={containerRef} style={{ height: 600 }} /> )}이 가이드는 어떠셨나요?