logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • 所有产品antv logo arrow
  • 3.x
  • 简介
  • 快速上手
  • 基础
    • 画布
    • 节点
    • 边
    • 连接桩
    • 交互
    • 事件
    • 数据
    • 动画
  • 进阶
    • 连接点
    • 工具
    • 群组
    • React 节点
    • Vue 节点
    • Angular 节点
    • HTML 节点
  • 插件
    • 图形变换
    • 对齐线
    • 复制粘贴
    • 快捷键
    • 撤销重做
    • 框选
    • 滚动画布
    • 小地图
    • Dnd
    • Stencil
    • 导出
  • 升级到 3.x 版本
  • 开发者工具

React 节点

上一篇
群组
下一篇
Vue 节点

资源

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库
WeaveFox-前端智能研发

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会
weavefoxWeaveFox-智能研发技术社区

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
weavefoxWeaveFox-前端智能研发
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

在本章节中主要介绍 React 节点相关的知识,通过阅读,你可以了解到

  • 如何使用 React 组件来渲染节点内容
  • 如何更新节点内容

渲染节点

我们提供了一个独立的包 @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)

Portal 方式

上述渲染方式有一个缺点:组件会被直接渲染到节点的 DOM 中,方式如下。

import { createRoot, Root } from 'react-dom/client'
const root = createRoot(container) // container 为节点容器
root.render(component)

此时组件不在常规渲染树中,因而无法访问外部 Context。若有此需求,请使用 Portal 模式来渲染 React 组件。