react-flow自定义节点

前言 在开发react-flow的过程中,自定义节点其实是一个很常用的功能,此文带大家一步一步定义自定义组件。 […]

前言

在开发react-flow的过程中,自定义节点其实是一个很常用的功能,此文带大家一步一步定义自定义组件。

 

自定义节点使用

新建组件

{firstOrLast === 0 ? "" : <Handle type="target" position={Position.Top} />}
<NodeWrapper
	onDoubleClick={onNodeDbClicked}
	style={{
		width: width ?? 200 + "px",
		height: height ?? 32 + "px"
	}}>
	{getNodeIcon(type, resStatus, pduStatus, pduSid)}
	<NodeInfoWrapper>{`${title ?? "名称获取失败"}  `}</NodeInfoWrapper>
</NodeWrapper>
{firstOrLast === 2 ? "" : <Handle type="source" position={Position.Bottom} />}

引入组件

import CuFlowNode from "@/components/Home/resTopo/topo/node";

 

定义节点

const nodeTypes = useMemo(() => ({ textUpdater: CuFlowNode }), []);
<ReactFlow
	nodeTypes={nodeTypes}
	nodes={nodes}
	edges={edges}
	onNodesChange={onNodesChange}
	onEdgesChange={onEdgesChange}
	onConnect={onConnect}
	onEdgeUpdate={onEdgeUpdate}
	onInit={setReactFlowInstance}></ReactFlow>

添加节点数据

nodeClone.push({
	id: computer.sid,
	type: "textUpdater",
	data: {
		sid: computer.sid,
		title: computer.alias,
		firstOrLast: 1
	},
	position: { x: 0, y: 0 }
});

 显示效果

 

 

版权声明
文章标题:react-flow自定义节点
文章链接:https://blog.chiyuba.com/qianduanjishu/534.html
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,转载或引用请注明出处。
温馨提示:本文最后更新于 2025年7月9日,部分内容可能存在时效性,请注意甄别。

相关推荐

更多教程
ssycms模板开发之模板文章列表 UI相关 ssycms模板开发之模板文章列表

  文章调用代码示例 id=&...

2 浏览
react-flow报错:Couldn’t create edge for source handle id: “undefined” 前端技术 react-flow报错:Couldn’t create edge for source handle id: “undefined”

前言 最近在使用react-flow中,...

6 浏览
ssycms模板开发之模板文件结构 UI相关 ssycms模板开发之模板文件结构

资源目录结构 ├─tempalte 模板...

2 浏览

评论