Comprehensive React Flow (@xyflow/react) patterns and best practices for building node-based UIs, workflow editors, and interactive diagrams. Use when working with React Flow for (1) building flow editors or node-based interfaces, (2) creating custom nodes and edges, (3) implementing drag-and-drop workflows, (4) optimizing performance for large graphs, (5) managing flow state and interactions, (6) implementing auto-layout or positioning, or (7) TypeScript integration with React Flow.
Comprehensive patterns and best practices for building production-ready node-based UIs with React Flow (@xyflow/react v12+).
Apply these guidelines when:
| Priority | Category | Focus | Prefix |
|---|---|---|---|
| 1 | Setup & Configuration | CRITICAL | setup- |
| 2 | Performance Optimization | CRITICAL | perf- |
| 3 | Node Patterns | HIGH | node- |
| 4 | Edge Patterns | HIGH | edge- |
| 5 | State Management | HIGH | state- |
| 6 | Hooks Usage | MEDIUM | hook- |
| 7 | Layout & Positioning | MEDIUM | layout- |
| 8 | Interaction Patterns | MEDIUM | interaction- |
| 9 | TypeScript Integration | MEDIUM | typescript- |
import { useCallback } from 'react';
import {
ReactFlow,
Background,
Controls,
MiniMap,
useNodesState,
useEdgesState,
addEdge
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
const initialNodes = [
{ id: '1', position: { x: 0, y: 0 }, data: { label: 'Node 1' } },
];
const initialEdges = [
{ id: 'e1-2', source: '1', target: '2' },
];
// Define outside component or use useMemo
const nodeTypes = { custom: CustomNode };
const edgeTypes = { custom: CustomEdge };
function Flow() {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params) => setEdges((eds) => addEdge(params, eds)),
[setEdges]
);
return (
<div style={{ width: '100%', height: '100vh'...