React Flow (@xyflow/react) for workflow visualization with custom nodes and edges. Use when building graph visualizations, creating custom workflow nodes, implementing edge labels, or controlling viewport. Triggers on ReactFlow, @xyflow/react, Handle, NodeProps, EdgeProps, useReactFlow, fitView.
React Flow (@xyflow/react) 是一个用于构建基于节点的图形、工作流编辑器和交互式图表的库。它提供了一个高度可定制的框架,用于创建可视化编程界面、流程流转和网络可视化。
bash
pnpm add @xyflow/react
typescript
import { ReactFlow, Node, Edge, Background, Controls, MiniMap } from @xyflow/react;
import @xyflow/react/dist/style.css;
const initialNodes: Node[] = [
{
id: 1,
type: input,
data: { label: 输入节点 },
position: { x: 250, y: 5 },
},
{
id: 2,
data: { label: 默认节点 },
position: { x: 100, y: 100 },
},
{
id: 3,
type: output,
data: { label: 输出节点 },
position: { x: 400, y: 100 },
},
];
const initialEdges: Edge[] = [
{ id: e1-2, source: 1, target: 2, animated: true },
{ id: e2-3, source: 2, target: 3 },
];
function Flow() {
return (
export default Flow;
节点是图形的基本构建块。每个节点具有:
typescript
import { Node } from @xyflow/react;
const node: Node = {
id: node-1,
type: default,
position: { x: 100, y: 100 },
data: { label: 节点标签 },
style: { background: #D6D5E6 },
className: custom-node,
};
内置节点类型:
边连接节点。每条边需要:
typescript
import { Edge } from @xyflow/react;
const edge: Edge = {
id: e1-2,
source: 1,
target: 2,
type: smoothstep,
animated: true,
label: 边标签,
style: { stroke: #fff, strokeWidth: 2 },
};
内置边类型:
连接点是节点上的连接点。使用 Position 枚举进行定位:
typescript
import { Handle, Position } from @xyflow/react;
可用位置:Position.Top、Position.Right、Position.Bottom、Position.Left
使用状态钩子实现完全控制:
typescript
import { useNodesState, useEdgesState, addEdge, OnConnect } from @xyflow/react;
import { useCallback } from react;
function ControlledFlow() {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect: OnConnect = useCallback(
(connection) => setEdges((eds) => addEdge(connection, eds)),
[setEdges]
);
return (
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
/>
);
}
访问 React Flow 实例以实现程序化控制:
typescript
import { useReactFlow } from @xyflow/react;
function FlowControls() {
const {
getNodes,
getEdges,
setNodes,
setEdges,
addNodes,
addEdges,
deleteElements,
fitView,
zoomIn,
zoomOut,
getNode,
getEdge,
updateNode,
updateEdge,
} = useReactFlow();
return (
);
}
使用带类型数据的 NodeProps
typescript
import { NodeProps, Node, Handle, Position } from @xyflow/react;
export type CustomNode = Node<{ label: string; status: active | inactive }, custom>;
function CustomNodeComponent({ data, selected }: NodeProps
return (
);
}
使用 nodeTypes 注册:
typescript
const nodeTypes: NodeTypes = { custom: CustomNodeComponent };
详细模式(包括样式、航空图钉和动态连接点)请参阅自定义节点参考。
使用 EdgeProps
typescript
import { BaseEdge, EdgeProps, getBezierPath } from @xyflow/react;
export type CustomEdge = Edge<{ status: normal | error }, custom>;
function CustomEdgeComponent(props: EdgeProps
const [edgePath] = getBezierPath(props);
return (
path={edgePath}
style={{ stroke: props.data?.status === error ? #ef4444 : #64748b }}
/>
);
}
所有工具均返回 [path, labelX, labelY, offsetX, offsetY]。
使用 EdgeLabelRenderer 实现支持指针事件的 HTML 标签:
typescript
import { EdgeLabelRenderer, BaseEdge, getBezierPath } from @xyflow/react;
function ButtonEdge(props: EdgeProps) {
const [edgePath, labelX, labelY] = getBezierPath(props);
return (
<>
position: absolute,
transform: translate(-50%, -50%) translate(${labelX}px, ${labelY}px),
pointerEvents: all,
}}
className=nodrag nopan
>
>
);
}
动画边、时间标签和 SVG 文本模式请参阅自定义边参考。
使用 useReactFlow() 钩子实现程序化视口控制:
typescript
import { useReactFlow } from @xyflow/react;
function ViewportControls() {
const { fitView, zoomIn, zoomOut, setCenter, screenToFlowPosition } = useReactFlow();
// 将所有节点适配到视图中
const handleFitView = () => fitView({ padding: 0.2, duration: 400 });
// 缩放控制
const handleZoomIn = () => zoomIn({ duration: 300 });
const handleZoomOut = () => zoom
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 react-flow-1775978042 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 react-flow-1775978042 技能
skillhub install react-flow-1775978042
文件大小: 14.76 KB | 发布时间: 2026-4-13 11:44