refactor(wrapNode): dont call dimension update twice on mount #1075
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
import React, { useEffect, useRef, memo, ComponentType, CSSProperties, useMemo, MouseEvent, useCallback } from 'react';
|
import React, {
|
||||||
|
useEffect,
|
||||||
|
useLayoutEffect,
|
||||||
|
useRef,
|
||||||
|
memo,
|
||||||
|
ComponentType,
|
||||||
|
CSSProperties,
|
||||||
|
useMemo,
|
||||||
|
MouseEvent,
|
||||||
|
useCallback,
|
||||||
|
} from 'react';
|
||||||
import { DraggableCore, DraggableData, DraggableEvent } from 'react-draggable';
|
import { DraggableCore, DraggableData, DraggableEvent } from 'react-draggable';
|
||||||
import cc from 'classcat';
|
import cc from 'classcat';
|
||||||
|
|
||||||
@@ -39,6 +49,7 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
|
|||||||
isDragging,
|
isDragging,
|
||||||
resizeObserver,
|
resizeObserver,
|
||||||
}: WrapNodeProps) => {
|
}: WrapNodeProps) => {
|
||||||
|
const observerInitialized = useRef<boolean>(false);
|
||||||
const updateNodeDimensions = useStoreActions((actions) => actions.updateNodeDimensions);
|
const updateNodeDimensions = useStoreActions((actions) => actions.updateNodeDimensions);
|
||||||
const addSelectedElements = useStoreActions((actions) => actions.addSelectedElements);
|
const addSelectedElements = useStoreActions((actions) => actions.addSelectedElements);
|
||||||
const updateNodePosDiff = useStoreActions((actions) => actions.updateNodePosDiff);
|
const updateNodePosDiff = useStoreActions((actions) => actions.updateNodePosDiff);
|
||||||
@@ -191,21 +202,22 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
|
|||||||
[node, onNodeDoubleClick]
|
[node, onNodeDoubleClick]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (nodeElement.current && !isHidden) {
|
// the resize observer calls an updateNodeDimensions initially.
|
||||||
|
// We don't need to force another dimension update if it hasn't happened yet
|
||||||
|
if (nodeElement.current && !isHidden && observerInitialized.current) {
|
||||||
updateNodeDimensions([{ id, nodeElement: nodeElement.current, forceUpdate: true }]);
|
updateNodeDimensions([{ id, nodeElement: nodeElement.current, forceUpdate: true }]);
|
||||||
}
|
}
|
||||||
}, [id, isHidden, sourcePosition, targetPosition]);
|
}, [id, isHidden, sourcePosition, targetPosition]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (nodeElement.current) {
|
if (nodeElement.current) {
|
||||||
|
observerInitialized.current = true;
|
||||||
const currNode = nodeElement.current;
|
const currNode = nodeElement.current;
|
||||||
resizeObserver?.observe(currNode);
|
resizeObserver?.observe(currNode);
|
||||||
|
|
||||||
return () => resizeObserver?.unobserve(currNode);
|
return () => resizeObserver?.unobserve(currNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (isHidden) {
|
if (isHidden) {
|
||||||
|
|||||||
Reference in New Issue
Block a user