React 节点
上一篇
群组
下一篇
Vue 节点
Loading...
我们提供了一个独立的包 @antv/x6-react-shape,用于通过 React 渲染节点。
版本兼容关系:X6 3.x 须使用 x6-react-shape 3.x 版本。同时 x6-react-shape 自 2.0.8 起仅支持 React 18 及以上;若项目低于 React 18,请将 x6-react-shape 锁定到 2.0.8 并使用 X6 2.x。
import { register } from '@antv/x6-react-shape'const NodeComponent = () => {return (<div className="react-node"><Progress type="circle" percent={30} width={80} /></div>)}register({shape: 'custom-basic-react-node',width: 100,height: 100,component: NodeComponent,})graph.addNode({shape: 'custom-basic-react-node',x: 60,y: 100,})
与 HTML 类似,注册节点时可提供 effect 字段(当前节点的 props 数组)。当 effect 中任一 prop 发生变化时,会重新渲染对应的 React 组件。
register({shape: 'custom-update-react-node',width: 100,height: 100,effect: ['data'],component: NodeComponent,})const node = graph.addNode({shape: 'custom-update-react-node',x: 60,y: 100,data: {progress: 30,},})setInterval(() => {const { progress } = node.getData<{ progress: number }>()node.setData({progress: (progress + 10) % 100,})}, 1000)
上述渲染方式有一个缺点:组件会被直接渲染到节点的 DOM 中,方式如下。
import { createRoot, Root } from 'react-dom/client'const root = createRoot(container) // container 为节点容器root.render(component)
此时组件不在常规渲染树中,因而无法访问外部 Context。若有此需求,请使用 Portal 模式来渲染 React 组件。