19 lines
518 B
JavaScript
19 lines
518 B
JavaScript
import { useState, useEffect } from 'react';
|
|
|
|
const MOBILE_BREAKPOINT = 768;
|
|
|
|
export function useIsMobile() {
|
|
const [isMobile, setIsMobile] = useState(
|
|
() => window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT}px)`).matches
|
|
);
|
|
|
|
useEffect(() => {
|
|
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT}px)`);
|
|
const handler = (e) => setIsMobile(e.matches);
|
|
mql.addEventListener('change', handler);
|
|
return () => mql.removeEventListener('change', handler);
|
|
}, []);
|
|
|
|
return isMobile;
|
|
}
|