Skip to content

Instantly share code, notes, and snippets.

@Dchole
Created March 19, 2021 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dchole/e73bb104ae8e9699f477ab91914b1d29 to your computer and use it in GitHub Desktop.
Save Dchole/e73bb104ae8e9699f477ab91914b1d29 to your computer and use it in GitHub Desktop.
Custom React hook for detecting user's device
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