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

View File

@@ -0,0 +1,5 @@
---
'@xyflow/react': patch
---
Fix fitView not working when onNodesChange is not defined.

View File

@@ -42,13 +42,16 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
if (hasDefaultNodes) {
setNodes(next);
} else if (onNodesChange) {
} else {
// When a controlled flow is used we need to collect the changes
const changes = getElementsDiffChanges({
items: next,
lookup: nodeLookup,
}) as NodeChange<NodeType>[];
// We only want to fire onNodesChange if there are changes to the nodes
if (changes.length > 0) {
onNodesChange(changes);
onNodesChange?.(changes);
} else if (fitViewQueued) {
// If there are no changes to the nodes, we still need to call setNodes
// 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 edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => {