Merge pull request #3064 from wbkd/refactor/useUpdateNodeInternals

refactor(useUpdateNodeInternals): only call updateNodeDimensions once
This commit is contained in:
Moritz Klack
2023-05-17 11:02:06 +02:00
committed by GitHub
3 changed files with 16 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@reactflow/core': patch
---
refactor(useUpdateNodeInternals): only call updateNodeDimensions once
@@ -23,7 +23,7 @@ const CustomNode: FC<NodeProps> = ({ id }) => {
<button
onClick={() => {
setHandleCount((c) => c + 1);
updateNodeInternals(id);
updateNodeInternals([id]);
}}
>
add handle
@@ -1,7 +1,7 @@
import { useCallback } from 'react';
import { useStoreApi } from '../hooks/useStore';
import type { UpdateNodeInternals } from '../types';
import type { NodeDimensionUpdate, UpdateNodeInternals } from '../types';
function useUpdateNodeInternals(): UpdateNodeInternals {
const store = useStoreApi();
@@ -10,16 +10,17 @@ function useUpdateNodeInternals(): UpdateNodeInternals {
const { domNode, updateNodeDimensions } = store.getState();
const updateIds = Array.isArray(id) ? id : [id];
const updates = updateIds.reduce<NodeDimensionUpdate[]>((res, updateId) => {
const nodeElement = domNode?.querySelector(`.react-flow__node[data-id="${updateId}"]`) as HTMLDivElement;
requestAnimationFrame(() => {
updateIds.forEach((updateId) => {
const nodeElement = domNode?.querySelector(`.react-flow__node[data-id="${updateId}"]`) as HTMLDivElement;
if (nodeElement) {
res.push({ id: updateId, nodeElement, forceUpdate: true });
}
if (nodeElement) {
updateNodeDimensions([{ id: updateId, nodeElement, forceUpdate: true }]);
}
});
});
return res;
}, []);
requestAnimationFrame(() => updateNodeDimensions(updates));
}, []);
}