Merge pull request #5132 from xyflow/fix/fit-view-no-on-nodes-change

Fix/fit view no on nodes change
This commit is contained in:
Moritz Klack
2025-04-01 12:41:35 +02:00
committed by GitHub
2 changed files with 11 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@xyflow/react': patch
---
Fix fitView not working when onNodesChange is not defined.
@@ -42,13 +42,16 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
if (hasDefaultNodes) { if (hasDefaultNodes) {
setNodes(next); setNodes(next);
} else if (onNodesChange) { } else {
// When a controlled flow is used we need to collect the changes
const changes = getElementsDiffChanges({ const changes = getElementsDiffChanges({
items: next, items: next,
lookup: nodeLookup, lookup: nodeLookup,
}) as NodeChange<NodeType>[]; }) as NodeChange<NodeType>[];
// We only want to fire onNodesChange if there are changes to the nodes
if (changes.length > 0) { if (changes.length > 0) {
onNodesChange(changes); onNodesChange?.(changes);
} else if (fitViewQueued) { } else if (fitViewQueued) {
// If there are no changes to the nodes, we still need to call setNodes // If there are no changes to the nodes, we still need to call setNodes
// to trigger a re-render and fitView. // to trigger a re-render and fitView.
@@ -61,6 +64,7 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
} }
} }
}, []); }, []);
const nodeQueue = useQueue<NodeType>(nodeQueueHandler); const nodeQueue = useQueue<NodeType>(nodeQueueHandler);
const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => { const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => {