返回顶部
r

react-flow-architecture

Architectural guidance for building node-based UIs with React Flow. Use when designing flow-based applications, making decisions about state management, integration patterns, or evaluating whether React Flow fits a use case.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.1.0
安全检测
已通过
119
下载量
0
收藏
概述
安装方式
版本历史

react-flow-architecture

# React Flow Architecture ## When to Use React Flow ### Good Fit - Visual programming interfaces - Workflow builders and automation tools - Diagram editors (flowcharts, org charts) - Data pipeline visualization - Mind mapping tools - Node-based audio/video editors - Decision tree builders - State machine designers ### Consider Alternatives - Simple static diagrams (use SVG or canvas directly) - Heavy real-time collaboration (may need custom sync layer) - 3D visualizations (use Three.js, react-three-fiber) - Graph analysis with 10k+ nodes (use WebGL-based solutions like Sigma.js) ## Architecture Patterns ### Package Structure (xyflow) ``` @xyflow/system (vanilla TypeScript) ├── Core algorithms (edge paths, bounds, viewport) ├── xypanzoom (d3-based pan/zoom) ├── xydrag, xyhandle, xyminimap, xyresizer └── Shared types @xyflow/react (depends on @xyflow/system) ├── React components and hooks ├── Zustand store for state management └── Framework-specific integrations @xyflow/svelte (depends on @xyflow/system) └── Svelte components and stores ``` **Implication**: Core logic is framework-agnostic. When contributing or debugging, check if issue is in @xyflow/system or framework-specific package. ### State Management Approaches #### 1. Local State (Simple Apps) ```tsx // useNodesState/useEdgesState for prototyping const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); ``` **Pros**: Simple, minimal boilerplate **Cons**: State isolated to component tree #### 2. External Store (Production) ```tsx // Zustand store example import { create } from 'zustand'; interface FlowStore { nodes: Node[]; edges: Edge[]; setNodes: (nodes: Node[]) => void; onNodesChange: OnNodesChange; } const useFlowStore = create<FlowStore>((set, get) => ({ nodes: initialNodes, edges: initialEdges, setNodes: (nodes) => set({ nodes }), onNodesChange: (changes) => { set({ nodes: applyNodeChanges(changes, get().nodes) }); }, })); // In component function Flow() { const { nodes, edges, onNodesChange } = useFlowStore(); return <ReactFlow nodes={nodes} onNodesChange={onNodesChange} />; } ``` **Pros**: State accessible anywhere, easier persistence/sync **Cons**: More setup, need careful selector optimization #### 3. Redux/Other State Libraries ```tsx // Connect via selectors const nodes = useSelector(selectNodes); const dispatch = useDispatch(); const onNodesChange = useCallback((changes: NodeChange[]) => { dispatch(nodesChanged(changes)); }, [dispatch]); ``` ### Data Flow Architecture ``` User Input → Change Event → Reducer/Handler → State Update → Re-render ↓ [Drag node] → onNodesChange → applyNodeChanges → setNodes → ReactFlow ↓ [Connect] → onConnect → addEdge → setEdges → ReactFlow ↓ [Delete] → onNodesDelete → deleteElements → setNodes/setEdges → ReactFlow ``` ### Sub-Flow Pattern (Nested Nodes) ```tsx // Parent node containing child nodes const nodes = [ { id: 'group-1', type: 'group', position: { x: 0, y: 0 }, style: { width: 300, height: 200 }, }, { id: 'child-1', parentId: 'group-1', // Key: parent reference extent: 'parent', // Key: constrain to parent position: { x: 10, y: 30 }, // Relative to parent data: { label: 'Child' }, }, ]; ``` **Considerations**: - Use `extent: 'parent'` to constrain dragging - Use `expandParent: true` to auto-expand parent - Parent z-index affects child rendering order ### Viewport Persistence ```tsx // Save viewport state const { toObject, setViewport } = useReactFlow(); const handleSave = () => { const flow = toObject(); // flow.nodes, flow.edges, flow.viewport localStorage.setItem('flow', JSON.stringify(flow)); }; const handleRestore = () => { const flow = JSON.parse(localStorage.getItem('flow')); setNodes(flow.nodes); setEdges(flow.edges); setViewport(flow.viewport); }; ``` ## Integration Patterns ### With Backend/API ```tsx // Load from API useEffect(() => { fetch('/api/flow') .then(r => r.json()) .then(({ nodes, edges }) => { setNodes(nodes); setEdges(edges); }); }, []); // Debounced auto-save const debouncedSave = useMemo( () => debounce((nodes, edges) => { fetch('/api/flow', { method: 'POST', body: JSON.stringify({ nodes, edges }), }); }, 1000), [] ); useEffect(() => { debouncedSave(nodes, edges); }, [nodes, edges]); ``` ### With Layout Algorithms ```tsx import dagre from 'dagre'; function getLayoutedElements(nodes: Node[], edges: Edge[]) { const g = new dagre.graphlib.Graph(); g.setGraph({ rankdir: 'TB' }); g.setDefaultEdgeLabel(() => ({})); nodes.forEach((node) => { g.setNode(node.id, { width: 150, height: 50 }); }); edges.forEach((edge) => { g.setEdge(edge.source, edge.target); }); dagre.layout(g); return { nodes: nodes.map((node) => { const pos = g.node(node.id); return { ...node, position: { x: pos.x, y: pos.y } }; }), edges, }; } ``` ## Performance Scaling ### Node Count Guidelines | Nodes | Strategy | |-------|----------| | < 100 | Default settings | | 100-500 | Enable `onlyRenderVisibleElements` | | 500-1000 | Simplify custom nodes, reduce DOM elements | | > 1000 | Consider virtualization, WebGL alternatives | ### Optimization Techniques ```tsx <ReactFlow // Only render nodes/edges in viewport onlyRenderVisibleElements={true} // Reduce node border radius (improves intersect calculations) nodeExtent={[[-1000, -1000], [1000, 1000]]} // Disable features not needed elementsSelectable={false} panOnDrag={false} zoomOnScroll={false} /> ``` ## Trade-offs ### Controlled vs Uncontrolled | Controlled | Uncontrolled | |------------|--------------| | More boilerplate | Less code | | Full state control | Internal state | | Easy persistence | Need `toObject()` | | Better for complex apps | Good for prototypes | ### Connection Modes | Strict (default) | Loose | |------------------|-------| | Source → Target only | Any handle → any handle | | Predictable behavior | More flexible | | Use for data flows | Use for diagrams | ```tsx <ReactFlow connectionMode={ConnectionMode.Loose} /> ``` ### Edge Rendering | Default edges | Custom edges | |---------------|--------------| | Fast rendering | More control | | Limited styling | Any SVG/HTML | | Simple use cases | Complex labels |

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 react-flow-architecture-1775982608 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 react-flow-architecture-1775982608 技能

通过命令行安装

skillhub install react-flow-architecture-1775982608

下载 Zip 包

⬇ 下载 react-flow-architecture v1.1.0

文件大小: 3.52 KB | 发布时间: 2026-4-13 11:44

v1.1.0 最新 2026-4-13 11:44
Version 1.1.0 of react-flow-architecture introduces clear, actionable guidance for building node-based UIs with React Flow.

- Lists use cases where React Flow excels and where alternatives should be considered.
- Details package structure and core architectural patterns (including state management options and the distinction between @xyflow/system and framework wrappers).
- Documents patterns for node/edge state handling (local state, Zustand, Redux), integrating with backends, viewport persistence, and layout algorithms.
- Provides guidance on optimizing performance at various scales, including rendering strategies and feature toggles.
- Explains advanced concepts such as sub-flow (nested node) architecture and trade-offs between controlled and uncontrolled components.
- Outlines integration best practices and charts for important configuration trade-offs (connection modes, edge rendering, etc.).

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部