Integrate in React
To use React components as custom components in the editor, see custom components.
Before integrating Univer into a React project, read installation and basic usage and make sure you understand React components and lifecycle hooks.
Integrating Web SDK with React 18 & 19
Integration steps
- Initialize Univer inside
useEffect. - Dispose of Univer in the cleanup function returned by
useEffect.
Preset configuration
UniverSheetsCorePreset
containerstring | HTMLElementDefault:'app'The element into which Univer is mounted, or the DOM ID of that element.
Example
Each mount uses its own child container. Cleanup defers disposal to a microtask, by which point React has finished its own cleanup, so nested React roots are never unmounted synchronously during a React commit.
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import { useEffect, useRef } from 'react'import '@univerjs/preset-sheets-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( UniverPresetSheetsCoreEnUS, ), }, presets: [ UniverSheetsCorePreset({ container, }), ], }) univerAPI.createWorkbook({}) return () => { queueMicrotask(() => { univer.dispose() container.remove() }) } }, []) return ( <div ref={containerRef} style={{ height: 600 }} /> )}Integrating Web SDK with React 16.9+ & 17
Notes before integration
Compatibility notes
Univer's view layer is built on React 18.3.1. React 16.9+ and 17 projects receive minimal compatibility support, and older React versions will not be supported indefinitely. Upgrade when possible; new features and the best experience assume a current version.
Build tool alias configuration
On React 16.9+ and 17, use your build tool's alias feature to provide the react-dom/client export. Without it, some features do not work.
Vite Configuration Example
export default defineConfig({ plugins: [react()], resolve: { alias: { 'react-dom/client': path.resolve(__dirname, './src/client.ts'), }, },})import ReactDOM from 'react-dom'export function createRoot(container: HTMLElement) { return { render: (element: JSX.Element) => { ReactDOM.render(element, container) }, }}CDN/UMD scene adaptation
If you load Univer and React with <script> tags, add the following code after React loads. The code provides the react-dom/client export:
<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>Integration steps
- Initialize Univer inside
useEffect. - Dispose of Univer in the cleanup function returned by
useEffect.
Example
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import { useEffect, useRef } from 'react'import '@univerjs/preset-sheets-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( UniverPresetSheetsCoreEnUS, ), }, presets: [ UniverSheetsCorePreset({ container, }), ], }) univerAPI.createWorkbook({}) return () => { queueMicrotask(() => { univer.dispose() container.remove() }) } }, []) return ( <div ref={containerRef} style={{ height: 600 }} /> )}How is this guide?