@clayui/shared
Version:
ClayShared component
23 lines (22 loc) • 605 B
JavaScript
import { useEffect, useState } from "react";
import { throttle } from "./throttle";
function getIsMobile() {
return window.document.body.clientWidth < 768;
}
function useIsMobileDevice() {
const [isMobile, setIsMobile] = useState(getIsMobile());
useEffect(() => {
if (typeof window === "undefined") {
return;
}
const handleResize = throttle(() => setIsMobile(getIsMobile()), 200);
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
};
}, []);
return isMobile;
}
export {
useIsMobileDevice
};