Skip to content

Commit

Permalink
Merge pull request #2148 from RunDevelopment/issue2147
Browse files Browse the repository at this point in the history
Fixed `setNodes` and `setEdges`
  • Loading branch information
moklick authored May 18, 2022
2 parents aab0858 + a61a6ac commit 44f1771
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/hooks/useReactFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { useCallback } from 'react';

import useViewportHelper from './useViewportHelper';
import { useStoreApi } from '../store';
import { ReactFlowInstance, Instance, NodeAddChange, EdgeAddChange, NodeResetChange, EdgeResetChange } from '../types';
import {
ReactFlowInstance,
Instance,
NodeAddChange,
EdgeAddChange,
NodeResetChange,
EdgeResetChange,
NodeRemoveChange,
EdgeRemoveChange,
} from '../types';

export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlowInstance<NodeData, EdgeData> {
const { initialized: viewportInitialized, ...viewportHelperFunctions } = useViewportHelper();
Expand Down Expand Up @@ -37,7 +46,10 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
if (hasDefaultNodes) {
setNodes(nextNodes);
} else if (onNodesChange) {
const changes = nextNodes.map((node) => ({ item: node, type: 'reset' } as NodeResetChange<NodeData>));
const changes =
nextNodes.length === 0
? nodes.map((node) => ({ type: 'remove', id: node.id } as NodeRemoveChange))
: nextNodes.map((node) => ({ item: node, type: 'reset' } as NodeResetChange<NodeData>));
onNodesChange(changes);
}
}, []);
Expand All @@ -49,7 +61,10 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
if (hasDefaultEdges) {
setEdges(nextEdges);
} else if (onEdgesChange) {
const changes = nextEdges.map((edge) => ({ item: edge, type: 'reset' } as EdgeResetChange<EdgeData>));
const changes =
nextEdges.length === 0
? edges.map((edge) => ({ type: 'remove', id: edge.id } as EdgeRemoveChange))
: nextEdges.map((edge) => ({ item: edge, type: 'reset' } as EdgeResetChange<EdgeData>));
onEdgesChange(changes);
}
}, []);
Expand Down

0 comments on commit 44f1771

Please sign in to comment.