Merge pull request #4718 from xyflow/fix/use-nodes-data

fix useNodesData returning undefined in svelte flow
This commit is contained in:
Moritz Klack
2024-10-09 17:32:19 +02:00
committed by GitHub
4 changed files with 10 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
---
'@xyflow/svelte': patch
---
Fixed hook useNodesData unexpectedly returning undefined

View File

@@ -21,7 +21,7 @@ export type UppercaseNode = Node<{ text: string }, 'uppercase'>;
export type MyNode = TextNode | ResultNode | UppercaseNode;
export function isTextNode(node: any): node is TextNode | UppercaseNode {
return node.type === 'text' || node.type === 'uppercase';
return !node || !node.type ? false : node.type === 'text' || node.type === 'uppercase';
}
const nodeTypes = {

View File

@@ -6,7 +6,7 @@
type ResultNodeType = Node<{}, 'result'>;
export function isTextNode(node: any): node is TextNodeType | UppercaseNode {
return node.type === 'text' || node.type === 'uppercase';
return !node || !node.type ? false : node.type === 'text' || node.type === 'uppercase';
}
export type MyNode = TextNodeType | UppercaseNodeType | ResultNodeType;

View File

@@ -22,6 +22,7 @@ export function useNodesData(nodeIds: any): any {
const { nodes, nodeLookup } = useStore();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let prevNodesData: any[] = [];
let initialRun = true;
return derived([nodes, nodeLookup], ([, nodeLookup], set) => {
const nextNodesData = [];
@@ -39,9 +40,10 @@ export function useNodesData(nodeIds: any): any {
}
}
if (!shallowNodeData(nextNodesData, prevNodesData)) {
if (!shallowNodeData(nextNodesData, prevNodesData) || initialRun) {
prevNodesData = nextNodesData;
set(isArrayOfIds ? nextNodesData : nextNodesData[0] ?? null);
initialRun = false;
}
});
}