addEdge()
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
edgeParams: EdgeType | Connectionedges: EdgeType[]options.getEdgeId?: GetEdgeIdCustom function to generate edge IDs. If not provided, the defaultgetEdgeIdfunction is used.options.onError?: OnErrorCalled when edge validation fails. If not provided, a default dev warning is used.
Returns
EdgeType[]
Notes
- 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.