Reviews React Flow code for anti-patterns, performance issues, and best practices. Use when reviewing code that uses @xyflow/react, checking for common mistakes, or optimizing node-based UI implementations.
问题: 导致所有节点在每次渲染时重新挂载。
tsx
// 错误 - 每次渲染都重新创建对象
function Flow() {
const nodeTypes = { custom: CustomNode }; // 错误
return
}
// 正确 - 在组件外部定义
const nodeTypes = { custom: CustomNode };
function Flow() {
return
}
// 正确 - 动态场景使用 useMemo
function Flow() {
const nodeTypes = useMemo(() => ({ custom: CustomNode }), []);
return
}
问题: 自定义组件在每次父组件更新时重新渲染。
tsx
// 错误 - 未使用记忆化
function CustomNode({ data }: NodeProps) {
return
// 正确 - 使用 memo 包裹
import { memo } from react;
const CustomNode = memo(function CustomNode({ data }: NodeProps) {
return
问题: 创建新的函数引用,破坏记忆化。
tsx
// 错误 - 内联回调
/>
// 正确 - 记忆化回调
const onNodesChange = useCallback(
(changes) => setNodes((nds) => applyNodeChanges(changes, nds)),
[]
);
tsx
// 错误 - 会抛出错误
function App() {
const { getNodes } = useReactFlow(); // 错误:没有 Provider
return
}
// 正确 - 在 Provider 内部使用
function FlowContent() {
const { getNodes } = useReactFlow(); // 正常工作
return
}
function App() {
return (
);
}
问题: 引用相等性检查失败,导致不必要的更新。
tsx
// 错误 - 每次创建新对象引用
setNodes(nodes.map(n => ({
...n,
data: { ...n.data, config: { nested: value } } // 每次都是新对象
})));
// 正确 - 使用 updateNodeData 进行针对性更新
const { updateNodeData } = useReactFlow();
updateNodeData(nodeId, { config: { nested: value } });
tsx
// 错误 - 没有高度,流程图不会渲染
// 正确 - 明确指定尺寸
tsx
// 默认样式必需
import @xyflow/react/dist/style.css;
tsx
// 错误 - 点击按钮会拖动节点
// 正确 - 阻止拖动
tsx
// 错误 - 字符串字面量
// 正确 - 类型安全的常量
import { Position } from @xyflow/react;
tsx
// 错误 - 直接修改
nodes[0].position = { x: 100, y: 100 };
setNodes(nodes);
// 正确 - 不可变更新
setNodes(nodes.map(n =>
n.id === 1 ? { ...n, position: { x: 100, y: 100 } } : n
));
tsx
// 错误 - 失去类型安全
const [nodes, setNodes] = useNodesState(initialNodes);
// 正确 - 明确指定类型
type MyNode = Node<{ value: number }, custom>;
const [nodes, setNodes] = useNodesState
tsx
// 错误 - 使用错误类型
function CustomNode(props: any) { ... }
// 正确 - 正确的 props 类型
function CustomNode(props: NodeProps
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 react-flow-code-review-1775978101 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 react-flow-code-review-1775978101 技能
skillhub install react-flow-code-review-1775978101
文件大小: 2.67 KB | 发布时间: 2026-4-13 11:44