diff --git a/examples/react/src/examples/ReconnectEdge/CustomNode.tsx b/examples/react/src/examples/ReconnectEdge/CustomNode.tsx
new file mode 100644
index 00000000..24e8b03c
--- /dev/null
+++ b/examples/react/src/examples/ReconnectEdge/CustomNode.tsx
@@ -0,0 +1,14 @@
+import { memo } from 'react';
+import { Handle, Position } from '@xyflow/react';
+
+const styles = { padding: '10px', background: 'green' };
+
+const CustomNode = ({ data }) => (
+ <>
+
{data.label}
+
+
+ >
+);
+
+export default memo(CustomNode);
diff --git a/packages/react/src/utils/changes.ts b/packages/react/src/utils/changes.ts
index df215340..f00fa7b3 100644
--- a/packages/react/src/utils/changes.ts
+++ b/packages/react/src/utils/changes.ts
@@ -19,10 +19,11 @@ function applyChanges(changes: any[], elements: any[]): any[] {
// By storing a map of changes for each element, we can a quick lookup as we
// iterate over the elements array!
const changesMap = new Map();
+ const newItems: any[] = [];
for (const change of changes) {
if (change.type === 'add') {
- updatedElements.push(change.item);
+ newItems.push(change);
continue;
} else if (change.type === 'remove' || change.type === 'replace') {
// For a 'remove' change we can safely ignore any other changes queued for
@@ -73,6 +74,12 @@ function applyChanges(changes: any[], elements: any[]): any[] {
updatedElements.push(updatedElement);
}
+ if (newItems.length) {
+ newItems.forEach((item) => {
+ updatedElements.splice(item.index, 0, { ...item.item });
+ });
+ }
+
return updatedElements;
}
@@ -237,7 +244,7 @@ export function getElementsDiffChanges({
const changes: any[] = [];
const itemsLookup = new Map(items.map((item) => [item.id, item]));
- for (const item of items) {
+ for (const [index, item] of items.entries()) {
const lookupItem = lookup.get(item.id);
const storeItem = lookupItem?.internals?.userNode ?? lookupItem;
@@ -246,7 +253,7 @@ export function getElementsDiffChanges({
}
if (storeItem === undefined) {
- changes.push({ item: item, type: 'add' });
+ changes.push({ item: item, type: 'add', index });
}
}
diff --git a/packages/system/src/types/changes.ts b/packages/system/src/types/changes.ts
index 5db0b6de..9b232733 100644
--- a/packages/system/src/types/changes.ts
+++ b/packages/system/src/types/changes.ts
@@ -32,6 +32,7 @@ export type NodeRemoveChange = {
export type NodeAddChange = {
item: NodeType;
type: 'add';
+ index?: number;
};
export type NodeReplaceChange = {
@@ -57,6 +58,7 @@ export type EdgeRemoveChange = NodeRemoveChange;
export type EdgeAddChange = {
item: EdgeType;
type: 'add';
+ index?: number;
};
export type EdgeReplaceChange = {