We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 80ab967 commit b1fc9a0Copy full SHA for b1fc9a0
1 file changed
src/hooks/use-mobile.ts
@@ -3,7 +3,12 @@ import * as React from 'react';
3
const MOBILE_BREAKPOINT = 768;
4
5
export function useIsMobile() {
6
- const [isMobile, setIsMobile] = React.useState<boolean>(false);
+ const [isMobile, setIsMobile] = React.useState<boolean>(() => {
7
+ if (typeof window === 'undefined') {
8
+ return false; // Default for SSR
9
+ }
10
+ return window.innerWidth < MOBILE_BREAKPOINT; // Initial value for client
11
+ });
12
13
React.useEffect(() => {
14
if (typeof window === 'undefined') {
@@ -14,8 +19,6 @@ export function useIsMobile() {
19
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
15
20
};
16
21
17
- checkIsMobile(); // Set initial value
18
-
22
const resizeObserver = new ResizeObserver(() => {
23
checkIsMobile();
24
});
0 commit comments