Skip to content

Commit

Permalink
Remove illegal setState
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreSi committed Oct 12, 2023
1 parent e246f6e commit f5fed4c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions newIDE/app/src/UI/TreeView/TreeViewRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ArrowHeadBottom from '../CustomSvgIcons/ArrowHeadBottom';
import ArrowHeadRight from '../CustomSvgIcons/ArrowHeadRight';
import Folder from '../CustomSvgIcons/Folder';
import ListIcon from '../ListIcon';
import useForceUpdate from '../../Utils/UseForceUpdate';
import './TreeView.css';
import {
shouldCloseOrCancel,
Expand Down Expand Up @@ -133,7 +134,8 @@ const TreeViewRow = <Item: ItemBaseAttributes>(props: Props<Item>) => {
} = data;
const node = flattenedData[index];
const left = node.depth * 15;
const [isStayingOver, setIsStayingOver] = React.useState<boolean>(false);
const forceUpdate = useForceUpdate();
const isStayingOverRef = React.useRef<boolean>(false);
const [
hasDelayPassedBeforeEditingName,
setHasDelayPassedBeforeEditingName,
Expand Down Expand Up @@ -179,14 +181,24 @@ const TreeViewRow = <Item: ItemBaseAttributes>(props: Props<Item>) => {
[onClick, openContextMenu]
);

const setIsStayingOver = React.useCallback(
(active: boolean) => {
if (active !== isStayingOverRef.current) {
isStayingOverRef.current = active;
forceUpdate();
}
},
[forceUpdate]
);

/**
* Effect that opens the node if the user is dragging another node and stays
* over the node.
*/
React.useEffect(
() => {
if (
isStayingOver &&
isStayingOverRef.current &&
!openWhenOverTimeoutId.current &&
node.canHaveChildren &&
node.collapsed
Expand All @@ -200,7 +212,9 @@ const TreeViewRow = <Item: ItemBaseAttributes>(props: Props<Item>) => {
};
}
},
[isStayingOver, onOpen, node]
// We want to have isStayingOverRef.current as dependency.
// eslint-disable-next-line react-hooks/exhaustive-deps
[onOpen, node, forceUpdate, isStayingOverRef.current]
);

/**
Expand Down

0 comments on commit f5fed4c

Please sign in to comment.