Skip to Content
示例Edges

多连接线

React Flow 通常一次只允许创建一个连接。本示例基于 自定义连接线 示例,展示如何从任意选定的节点一次性绘制多条连接线。

🌐 React Flow typically only allows one connection to be created at a time. This example builds on the custom connection line example to show how to draw multiple connection lines from any selected nodes at once.

注意 onConnect 处理程序。如果你忘记包含它,那么即使选择了多个节点,也只会创建一个连接!

import { useCallback } from 'react'; import { ReactFlow, Background, useNodesState, useEdgesState, addEdge, } from '@xyflow/react'; import '@xyflow/react/dist/style.css'; import ConnectionLine from './ConnectionLine'; const initialNodes = [ { id: 'a', type: 'input', data: { label: 'Click to select' }, position: { x: 100, y: -100 }, }, { id: 'b', type: 'input', data: { label: 'these nodes' }, position: { x: 300, y: -50 }, }, { id: 'c', type: 'input', data: { label: 'then drag... ' }, position: { x: 150, y: 0 }, }, { id: 'd', type: 'output', data: { label: '...and connect to me!' }, position: { x: 250, y: 200 }, }, ]; const ConnectionLineFlow = () => { const [nodes, _, onNodesChange] = useNodesState(initialNodes); const [edges, setEdges, onEdgesChange] = useEdgesState([]); const onConnect = useCallback( ({ source, target }) => { return setEdges((eds) => nodes .filter((node) => node.id === source || node.selected) .reduce( (eds, node) => addEdge({ source: node.id, target }, eds), eds, ), ); }, [nodes], ); return ( <ReactFlow nodes={nodes} edges={edges} onNodesChange={onNodesChange} onEdgesChange={onEdgesChange} connectionLineComponent={ConnectionLine} onConnect={onConnect} fitView fitViewOptions={{ padding: 0.2, }} > <Background /> </ReactFlow> ); }; export default ConnectionLineFlow;

这个示例使用了特殊的 internalsSymbol 来访问你通常不需要访问的节点属性。隐藏在这个符号后的属性没有像公共 API 那样的稳定性保证,因此请谨慎使用它们。

Last updated on