0 points

useDeviceDetect Hook


dashboard

Wed Oct 21 2020 17:34:45 GMT+0000 (UTC)

Posted by @raulingg #react.js #hooks

// utils/useDeviceDetect.js
import React from "react";

export default function useDeviceDetect() {
  const [isMobile, setMobile] = React.useState(false);

  React.useEffect(() => {
    const userAgent =
      typeof window.navigator === "undefined" ? "" : navigator.userAgent;
    const mobile = Boolean(
      userAgent.match(
        /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i
      )
    );
    setMobile(mobile);
  }, []);

  return { isMobile };
}
content_copy Copy

react hooks

https://medium.com/code-artistry/how-to-create-a-custom-usedevicedetect-react-hook-f5a1bfe64599