Merge pull request #3064 from wbkd/refactor/useUpdateNodeInternals
refactor(useUpdateNodeInternals): only call updateNodeDimensions once
This commit is contained in:
@@ -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));
|
||||
}, []);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user