@findify/react-components
Version:
Findify react UI components
21 lines (16 loc) • 571 B
text/typescript
import { useState, useEffect, useRef } from 'react';
import { useConfig } from '@findify/react-connect';
export const useMobile = () => {
const { config } = useConfig();
const mql = useRef(
window.matchMedia(`(min-width: ${config.get('mobileBreakpoint')}px)`)
);
const getValue = () => !mql.current.matches;
const [isMobile, setIsMobile] = useState(getValue);
useEffect(() => {
const handler = () => setIsMobile(getValue);
mql.current.addListener(handler);
return () => mql.current.removeListener(handler);
}, []);
return isMobile;
};