react17后,官方推荐使用函数组件。
import React, { useEffect, useState} from 'react'
import { Graph } from '@antv/x6';function Myeditor() {// 初始,mounted生命周期时, container组件还没挂载。const [container,setcontainer] = useState<HTMLElement>(ateElement('div'));const data = {// 节点nodes: [{id: 'node1', // String,可选,节点的唯一标识x: 40, // Number,必选,节点位置的 x 值y: 40, // Number,必选,节点位置的 y 值width: 80, // Number,可选,节点大小的 width 值height: 40, // Number,可选,节点大小的 height 值label: 'hello', // String,节点标签},{id: 'node2', // String,节点的唯一标识x: 160, // Number,必选,节点位置的 x 值y: 180, // Number,必选,节点位置的 y 值width: 80, // Number,可选,节点大小的 width 值height: 40, // Number,可选,节点大小的 height 值label: 'world', // String,节点标签},],// 边edges: [{source: 'node1', // String,必须,起始节点 idtarget: 'node2', // String,必须,目标节点 id},],};// 获取container对象const refContainer=(container: HTMLDivElement)=>{setcontainer(container);}//官方文档写的是componentDidMount,因为react取消了三个生命周期函数,所以使用useEffectuseEffect(()=>{const graph=(new Graph({container: container,width: 800,height: 600,}));graph.fromJSON(data);}
)return (<div><div id="container" ref={refContainer}></div></div>)
}
export default Myeditor;
详细可以看antvis/X6/react-demo
本文发布于:2024-02-04 09:52:13,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170704511554527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |