Skip to content

Commit a30f569

Browse files
committed
Fix warning
1 parent 9424433 commit a30f569

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/utils/hooks/useIsMobile.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import { useLayoutEffect, useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import debounce from 'lodash/debounce';
33

44
const useIsMobile = (): boolean => {
5-
const [isMobile, setIsMobile] = useState(false);
5+
// Initialize with null to avoid hydration mismatch
6+
const [isMobile, setIsMobile] = useState<boolean | null>(null);
67

7-
useLayoutEffect(() => {
8-
const updateSize = (): void => {
8+
useEffect(() => {
9+
// Skip effect on server side
10+
if (typeof window === 'undefined') return;
11+
12+
const updateSize = debounce((): void => {
913
setIsMobile(window.innerWidth < 768);
10-
};
11-
window.addEventListener('resize', debounce(updateSize, 250));
14+
}, 250);
15+
16+
// Initial check
1217
updateSize();
1318

19+
window.addEventListener('resize', updateSize);
1420
return (): void => window.removeEventListener('resize', updateSize);
1521
}, []);
1622

17-
return isMobile;
23+
// Return false during SSR, actual value after hydration
24+
return isMobile ?? false;
1825
};
1926

2027
export default useIsMobile;

0 commit comments

Comments
 (0)