不受控制的流程
🌐 Uncontrolled Flow
使用 React Flow 有两种方式——受控或非受控。受控意味着你可以控制节点和边的状态。在非受控流中,节点和边的状态由 React Flow 内部处理。在本部分中,我们将向你展示如何使用非受控流。
🌐 There are two ways to use React Flow - controlled or uncontrolled. Controlled means, that you are in control of the state of the nodes and edges. In an uncontrolled flow the state of the nodes and edges is handled by React Flow internally. In this part we will show you how to work with an uncontrolled flow.
不受控制的流的实现更简单,因为你不需要传递任何处理程序:
🌐 An implementation of an uncontrolled flow is simpler, because you don’t need to pass any handlers:
正如你所看到的,我们正在传递 defaultEdgeOptions 来定义边缘是动画的。这很有帮助,因为你不能再使用 onConnect 处理程序向新创建的边缘传递自定义选项。尝试将“节点 B”与“节点 C”连接,你会看到新边缘是动画的。
🌐 As you can see, we are passing defaultEdgeOptions to define that edges are animated. This is helpful, because you can’t use the onConnect handler anymore to pass custom options to a newly created edge. Try to connect “Node B” with “Node C” and you see that the new edge is animated.
更新节点和边
🌐 Updating nodes and edges
由于你的本地状态中没有节点和边,你无法直接更新它们。为此,你需要使用带有更新内部状态函数的 React Flow 实例。你可以通过 onInit 回调接收该实例,或者更好地使用 useReactFlow hook。让我们创建一个按钮,在随机位置添加一个新节点。为此,我们将流程封装在 ReactFlowProvider 中,并使用 addNodes 函数。
🌐 Since you don’t have nodes and edges in your local state, you can’t update them directly. To do so, you need to use the React Flow instance that comes with functions for updating the internal state. You can receive the instance via the onInit callback or better by using the useReactFlow hook. Let’s create a button that adds a new node at a random position. For this, we are wrapping our flow with the ReactFlowProvider and use the addNodes function.
在此示例中,Flow 组件被 ReactFlowProvider 封装,以使用 useReactFlow 钩子。