Files
vercel__flags/examples/snippets/hooks/use-mobile.tsx
Dominik Ferber 8152ef3371 split up docs (#7)
* wip

* init collection

* convert identify

* convert precompute

* convert adapters

* convert marketing-pages

* convert feature-flags-in-edge-middleware

* remove docs

* rename to snippets

* rename collection to snippets

* eslint

* list examples on landing page

* remove unused code
2024-12-18 11:27:26 +02:00

22 lines
585 B
TypeScript

import * as React from 'react';
const MOBILE_BREAKPOINT = 768;
export function useIsMobile() {
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
undefined,
);
React.useEffect(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
};
mql.addEventListener('change', onChange);
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
return () => mql.removeEventListener('change', onChange);
}, []);
return !!isMobile;
}