添加交互性
¥Adding Interactivity
现在我们已经构建了 第一个流程,让我们添加交互功能,以便你可以选择、拖动和删除节点和边。
¥Now that we’ve built our first flow, let’s add interactivity so you can select, drag, and remove nodes and edges.
处理变化事件
¥Handling change events
默认情况下,React Flow 除了处理视口外,不管理任何内部状态更新。与 HTML <input />
元素一样,你需要将 事件处理程序 传递给 React Flow,才能将触发的更改应用于节点和边。
¥By default React Flow doesn’t manage any internal state updates besides handling the
viewport. As you would with an HTML <input />
element you need to pass
event handlers to React Flow in order to apply
triggered changes to your nodes and edges.
添加导入
¥Add imports
为了管理变更,我们将使用 useState
和 React Flow 中的两个辅助函数:applyNodeChanges
和 applyEdgeChanges
。因此,让我们导入以下函数:
¥To manage changes, we’ll be using useState
with two helper functions from React Flow:
applyNodeChanges
and
applyEdgeChanges
. So let’s import these
functions:
import { useState, useCallback } from 'react';
import { ReactFlow, applyEdgeChanges, applyNodeChanges } from '@xyflow/react';
定义节点和边缘
¥Define nodes and edges
我们需要定义初始节点和边。这些将是我们流程的起点。
¥We need to define initial nodes and edges. These will be the starting point for our flow.
const initialNodes = [
{
id: 'n1',
position: { x: 0, y: 0 },
data: { label: 'Node 1' },
type: 'input',
},
{
id: 'n2',
position: { x: 100, y: 100 },
data: { label: 'Node 2' },
},
];
const initialEdges = [
{
id: 'n1-n2',
source: 'n1',
target: 'n2',
},
];
初始化状态
¥Initialize state
在我们的组件中,我们将调用 useState
钩子来管理节点和边的状态:
¥In our component, we’ll call the useState
hook to manage the state of our nodes and
edges:
export default function App() {
const [nodes, setNodes] = useState(initialNodes);
const [edges, setEdges] = useState(initialEdges);
return (
<div style={{ height: '100%', width: '100%' }}>
<ReactFlow>
<Background />
<Controls />
</ReactFlow>
</div>
);
}
添加事件处理程序
¥Add event handlers
我们需要创建两个事件处理程序:onNodesChange
和 onEdgesChange
。当发生变化(例如拖动或删除元素)时,它们将用于更新节点和边的状态。请继续将这些处理程序添加到你的组件中:
¥We need to create two event handlers:
onNodesChange
and
onEdgesChange
. They will be used to update
the state of our nodes and edges when changes occur, such as dragging or deleting an
element. Go ahead and add these handlers to your component:
const onNodesChange = useCallback(
(changes) => setNodes((nodesSnapshot) => applyNodeChanges(changes, nodesSnapshot)),
[],
);
const onEdgesChange = useCallback(
(changes) => setEdges((edgesSnapshot) => applyEdgeChanges(changes, edgesSnapshot)),
[],
);
将它们传递给 ReactFlow
¥Pass them to ReactFlow
现在我们可以将节点、边和事件处理程序传递给 <ReactFlow />
组件:
¥Now we can pass our nodes, edges, and event handlers to the <ReactFlow />
component:
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
fitView
>
<Background />
<Controls />
</ReactFlow>
交互式流程
¥Interactive flow
就是这样!现在你有了一个基本的交互式流程 🎉
¥And that’s it! You now have a basic interactive flow 🎉
当你拖动或选择一个节点时,会触发 onNodesChange
处理程序。applyNodeChanges
函数随后使用这些更改事件来更新节点的当前状态。以下是它们是如何组合在一起的。尝试点击并拖动节点来移动它,并实时观察 UI 更新。
¥When you drag or select a node, the onNodesChange
handler is triggered. The
applyNodeChanges
function then uses these change events to update the current state of
your nodes. Here’s how it all comes together. Try clicking and dragging a node to move it
around and watch the UI update in real time.
处理连接
¥Handling connections
缺少最后一部分:以交互方式连接节点。为此,我们需要实现一个 onConnect
处理程序。
¥One last piece is missing: connecting nodes interactively. For this, we need to implement
an onConnect
handler.
创建 onConnect
处理程序
¥Create onConnect
handler
每当两个节点之间建立新连接时,都会调用 onConnect
处理程序。我们可以使用 addEdge
实用函数创建新边并更新边数组。
¥The onConnect
handler is called whenever a new connection is made between two nodes. We
can use the addEdge
utility function to create a new
edge and update the edge Array.
const onConnect = useCallback(
(params) => setEdges((edgesSnapshot) => addEdge(params, edgesSnapshot)),
[],
);
传递给 ReactFlow
¥Pass it to ReactFlow
现在我们可以将 onConnect
处理程序传递给 <ReactFlow />
组件:
¥Now we can pass the onConnect
handler to the <ReactFlow />
component:
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
fitView
>
<Background />
<Controls />
</ReactFlow>
可连接流程
¥Connectable flow
尝试通过将一个句柄拖到另一个句柄来连接两个节点。onConnect
处理程序将被触发,新的边将添加到流中。🥳
¥Try to connect the two nodes by dragging from on handle to another one. The onConnect
handler will be triggered, and the new edge will be added to the flow. 🥳
完整代码示例 🏁
¥Full code example 🏁
这里发生了什么?每当 React Flow 触发更改(节点初始化、节点拖动、边缘选择等)时,都会调用 onNodesChange
处理程序。我们导出 applyNodeChanges
处理程序,这样你就不需要自己处理更改。applyNodeChanges
处理程序返回更新的节点数组,即你的新节点数组。现在你有一个具有以下功能的交互式流程:
¥What is happening here? Whenever React Flow triggers a change (node init, node drag, edge
select, etc.), the onNodesChange
handler gets called. We export an applyNodeChanges
handler so that you don’t need to handle the changes by yourself. The applyNodeChanges
handler returns an updated array of nodes that is your new nodes array. You now have an
interactive flow with the following capabilities:
-
可选节点和边缘
¥selectable nodes and edges
-
可拖动节点
¥draggable nodes
-
通过将节点句柄从一个节点拖到另一个节点句柄来连接节点
¥connectable nodes by dragging from one node handle to another
-
按下
shift
可显示多选区域 - 默认为selectionKeyCode
¥multi-selection area by pressing
shift
— the defaultselectionKeyCode
-
按下
cmd
可进行多选 - 默认为multiSelectionKeyCode
¥multi-selection by pressing
cmd
— the defaultmultiSelectionKeyCode
-
按下
backspace
可移除选定元素 - 默认为deleteKeyCode
¥removing selected elements by pressing
backspace
— the defaultdeleteKeyCode
如果你想直接创建自己的应用,我们建议你查看 自定义 部分。否则,请继续阅读以了解有关 React Flows 功能的更多信息。
¥If you want to jump straight into creating your own application, we recommend checking out the Customization section. Otherwise keep reading to learn more about React Flows capabilities.