From fc95eecc11900b924517e5e1af46099f439e1f29 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sat, 1 Apr 2023 23:21:44 +0800 Subject: [PATCH] fix: mobile side bar auto collapsing --- src/components/Menu/Menu.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index f6abf85..f74a897 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -1,4 +1,5 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useRef } from 'react'; + import useStore from '@store/store'; import NewChat from './NewChat'; @@ -13,10 +14,16 @@ const Menu = () => { const hideSideMenu = useStore((state) => state.hideSideMenu); const setHideSideMenu = useStore((state) => state.setHideSideMenu); + const windowWidthRef = useRef(window.innerWidth); + useEffect(() => { if (window.innerWidth < 768) setHideSideMenu(true); window.addEventListener('resize', () => { - if (window.innerWidth < 768) setHideSideMenu(true); + if ( + windowWidthRef.current !== window.innerWidth && + window.innerWidth < 768 + ) + setHideSideMenu(true); }); }, []);