chore: fix linting errors and ts bugs.
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useSyncExternalStore } from 'react';
|
||||
|
||||
export function useIsMobile(breakpoint = 768): boolean {
|
||||
const [isMobile, setIsMobile] = useState(() => window.innerWidth < breakpoint);
|
||||
useEffect(() => {
|
||||
const mq = window.matchMedia(`(max-width: ${breakpoint - 1}px)`);
|
||||
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);
|
||||
mq.addEventListener('change', handler);
|
||||
setIsMobile(mq.matches);
|
||||
return () => mq.removeEventListener('change', handler);
|
||||
}, [breakpoint]);
|
||||
return isMobile;
|
||||
const query = `(max-width: ${breakpoint - 1}px)`;
|
||||
|
||||
return useSyncExternalStore(
|
||||
(onStoreChange) => {
|
||||
const mq = window.matchMedia(query);
|
||||
mq.addEventListener('change', onStoreChange);
|
||||
return () => mq.removeEventListener('change', onStoreChange);
|
||||
},
|
||||
() => window.matchMedia(query).matches,
|
||||
() => false,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user