Loading View...
Deep dive into identifying and fixing unnecessary re-renders in a React Flow-based node editor.
The node editor was dropping frames during drag operations. 60fps β 30fps when more than 20 nodes were on the canvas.
Used React DevTools Profiler to identify the bottleneck:
onNodesChange callback was creating new node objects on each call// Before: inline object creation
const handleNodesChange = (changes: NodeChange[]) => {
setNodes((nds) => applyNodeChanges(changes, nds));
};
// After: stable callback reference
const handleNodesChange = useCallback(
(changes: NodeChange[]) => {
setNodes((nds) => applyNodeChanges(changes, nds));
},
[setNodes]
);
Also wrapped all custom node components in React.memo with proper comparison functions.
Back to consistent 60fps with 50+ nodes on canvas. The key insight: React Flow internally optimizes, but your custom node components bypass those optimizations if they're not properly memoized.
Always profile before optimizing. The bottleneck was not where I expected, I initially suspected the connection line rendering.