Created
March 19, 2021 19:10
-
-
Save Dchole/e73bb104ae8e9699f477ab91914b1d29 to your computer and use it in GitHub Desktop.
Custom React hook for detecting user's device
This file contains 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"; | |
export type TDevice = "mobile" | "desktop"; | |
const useDevice = () => { | |
const [device, setDevice] = useState<TDevice>("mobile"); | |
useEffect(() => { | |
const { userAgent } = navigator; | |
userAgent.includes("Mobi") ? setDevice("mobile") : setDevice("desktop"); | |
}, []); | |
return device; | |
}; | |
export default useDevice; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment