Skip to content

Commit

Permalink
fix: support react 17 (#2968)
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector authored Nov 29, 2022
1 parent cd85c5b commit 94f9042
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/x6-react-shape/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/x6-react-shape",
"version": "2.0.4",
"version": "2.0.5",
"description": "X6 shape for rendering react components.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down Expand Up @@ -39,8 +39,8 @@
},
"peerDependencies": {
"@antv/x6": "^2.x",
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react": ">=16.8.6 || >=17.0.0 || >=18.0.0",
"react-dom": ">=16.8.6 || >=17.0.0 || >= 18.0.0"
},
"devDependencies": {
"@antv/x6": "^2.x",
Expand Down
31 changes: 23 additions & 8 deletions packages/x6-react-shape/src/view.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React, { ReactPortal } from 'react'
import { createPortal } from 'react-dom'
import { createRoot, Root } from 'react-dom/client'
import React, { ReactPortal, version as reactVersion } from 'react'
import ReactDOM, { createPortal } from 'react-dom'
import type { Root, createRoot as CreateRoot } from 'react-dom/client'
import { Dom, NodeView } from '@antv/x6'
import { ReactShape } from './node'
import { Portal } from './portal'
import { Wrap } from './wrap'

const [, major] = /^(\d+)\.\d+\.\d+$/.exec(reactVersion)!
const reactMajor = Number(major)
const isPreEighteen = reactMajor < 18
export class ReactShapeView extends NodeView<ReactShape> {
root?: Root

Expand All @@ -31,16 +34,28 @@ export class ReactShapeView extends NodeView<ReactShape> {
const portal = createPortal(elem, container) as ReactPortal
Portal.connect(this.cell.id, portal)
} else {
const root = createRoot(container)
root.render(elem)
if (isPreEighteen) {
ReactDOM.render(elem, container)
} else {
// eslint-disable-next-line
const createRoot = require('react-dom/client')
.createRoot as typeof CreateRoot
this.root = createRoot(container)
this.root.render(elem)
}
}
}
}

protected unmountReactComponent() {
if (this.root) {
this.root.unmount()
this.root = undefined
const container = this.getComponentContainer()
if (container) {
if (isPreEighteen) {
ReactDOM.unmountComponentAtNode(container)
} else if (this.root) {
this.root.unmount()
this.root = undefined
}
}
}

Expand Down

0 comments on commit 94f9042

Please sign in to comment.