addEdge()
此实用程序是一个便利函数,用于将新的 Edge 添加到边数组中。它还执行一些验证以确保你不会添加无效边缘或重复现有边缘。
¥This util is a convenience function to add a new Edge to an
array of edges. It also performs some validation to make sure you don’t add an
invalid edge or duplicate an existing one.
import { useCallback } from 'react';
import {
ReactFlow,
addEdge,
useNodesState,
useEdgesState,
} from '@xyflow/react';
export default function Flow() {
const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
const onConnect = useCallback(
(connection) => {
setEdges((oldEdges) => addEdge(connection, oldEdges));
},
[setEdges],
);
return <ReactFlow nodes={nodes} edges={edges} onConnect={onConnect} />;
}签名
¥Signature
Parameters:| Name | Type | Default |
|---|---|---|
edgeParams | EdgeType | ConnectionEither an | |
edges | EdgeType[]The array of all current edges. | |
options.getEdgeId | GetEdgeIdCustom function to generate edge IDs. If not provided, the default |
注释
¥Notes
-
如果已经存在具有相同
target和source的边(如果设置了相同的targetHandle和sourceHandle),那么即使id属性不同,此实用程序也不会添加新边。¥If an edge with the same
targetandsourcealready exists (and the sametargetHandleandsourceHandleif those are set), then this util won’t add a new edge even if theidproperty is different.
Last updated on