How to Check if UserAgent Is a Mobile Device

0 minute read

userAgent is a string value which will be set to any one of the following device names if they are using a mobile device:

Android, BlackBerry, iPhone, iPad, iPod, Opera Mini, IEMobile, or WPDesktop.

 1export default function useDeviceDetect() {
 2  const [isMobile, setMobile] = React.useState(false);
 3
 4  React.useEffect(() => {
 5    const userAgent =
 6      typeof window.navigator === "undefined" ? "" : navigator.userAgent;
 7    const mobile = Boolean(
 8      userAgent.match(
 9        /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i
10      )
11    );
12    setMobile(mobile);
13  }, []);
14}