Last active
November 13, 2021 13:27
-
-
Save MauricioRobayo/24ea30e147d16a0d54867fd0c725ffe5 to your computer and use it in GitHub Desktop.
React hook to detect the device type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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