Skip to content

Instantly share code, notes, and snippets.

@MauricioRobayo
Last active November 13, 2021 13:27
Show Gist options
  • Select an option

  • Save MauricioRobayo/24ea30e147d16a0d54867fd0c725ffe5 to your computer and use it in GitHub Desktop.

Select an option

Save MauricioRobayo/24ea30e147d16a0d54867fd0c725ffe5 to your computer and use it in GitHub Desktop.
React hook to detect the device type
import { useEffect, useState } from "react";
const useDeviceDetect = () => {
const [isMobile, setIsMobile] = useState<boolean | null>(null);
const mobileRegExp = new RegExp(
"Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop",
"i"
);
useEffect(() => {
const userAgent = window.navigator?.userAgent || "";
setIsMobile(mobileRegExp.test(userAgent));
}, []);
return { isMobile };
};
export default useDeviceDetect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment