Skip to content

Commit 162fd2b

Browse files
committed
moves all unmount code inside useEffectEvent
1 parent 9eefd5e commit 162fd2b

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

src/pages/Search/SearchTypeMenuWide.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,18 @@ function Section({section, hash, activeItemIndex, sectionStartIndex, reportCount
6363
]);
6464

6565
const [isExpanded, setIsExpanded] = useState(true);
66-
const isExpandedRef = useRef(isExpanded);
6766

68-
const onCollapsedCallback = useEffectEvent(onCollapsed);
69-
70-
useEffect(() => {
71-
isExpandedRef.current = isExpanded;
72-
}, [isExpanded]);
67+
const onUnmount = useEffectEvent(() => {
68+
if (isExpanded) {
69+
return;
70+
}
71+
// When the section is removed/unmounted while collapsed,
72+
// notify the parent that the section is no longer collapsed.
73+
onCollapsed(false);
74+
});
7375

74-
// When the section is removed/unmounted while collapsed,
75-
// notify the parent that the section is no longer collapsed.
7676
useEffect(() => {
77-
return () => {
78-
if (isExpandedRef.current) {
79-
return;
80-
}
81-
onCollapsedCallback(false);
82-
};
77+
return () => onUnmount();
8378
}, []);
8479

8580
return (

0 commit comments

Comments
 (0)