Skip to Content
教程核心概念添加交互性

添加交互性

🌐 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.

添加导入

为了管理更改,我们将使用 useState 和来自 React Flow 的两个辅助函数:applyNodeChangesapplyEdgeChanges。所以让我们导入这些函数:

🌐 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

我们需要创建两个事件处理器: onNodesChangeonEdgesChange。当发生变化时(例如拖动或删除元素),它们将用于更新我们的节点和边的状态。请继续将这些处理器添加到你的组件中:

🌐 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 函数使用这些更改事件来更新节点的当前状态。以下是整个过程的详细说明。试着点击并拖动一个节点来移动它,并实时观察界面的更新。

🌐 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:

  • 可选节点和边缘
  • 可拖动节点
  • 通过将节点句柄从一个节点拖到另一个节点句柄来连接节点
  • 通过按 shift 进行多选区域 — 默认 selectionKeyCode
  • 通过按 cmd 进行多选 — 默认 multiSelectionKeyCode
  • 通过按 backspace 删除选定的元素 — 默认 deleteKeyCode

如果你想直接开始创建你自己的应用,我们建议查看自定义部分。否则,请继续阅读以了解更多关于 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.

Last updated on