Skip to content

Commit

Permalink
move popper destroy to unmount component
Browse files Browse the repository at this point in the history
  • Loading branch information
azouaoui-med committed May 31, 2020
1 parent dcdb712 commit 70393ca
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/ProSidebar/Menu/SubMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,16 @@ const SubMenu: React.ForwardRefRenderFunction<unknown, Props> = (
let popperInstance;
const { collapsed, rtl } = useContext(SidebarContext);
const [closed, setClosed] = useState(!defaultOpen);
const popperElRef = useRef();
const referenceElement = useRef();
const popperElement = useRef();
const popperElRef = useRef(null);
const referenceElement = useRef(null);
const popperElement = useRef(null);

const handleToggleSubMenu = () => {
setClosed(!closed);
};

useEffect(() => {
if (firstchild) {
if (popperInstance) {
popperInstance.destroy();
popperInstance = null;
}

if (referenceElement.current && popperElement.current) {
popperInstance = createPopper(referenceElement.current, popperElement.current, {
placement: 'right',
Expand All @@ -75,6 +70,13 @@ const SubMenu: React.ForwardRefRenderFunction<unknown, Props> = (
ro.observe(referenceElement.current);
}
}

return () => {
if (popperInstance) {
popperInstance.destroy();
popperInstance = null;
}
};
}, [collapsed, rtl]);

const subMenuRef: LegacyRef<HTMLLIElement> = (ref as any) || React.createRef<HTMLLIElement>();
Expand Down

0 comments on commit 70393ca

Please sign in to comment.