Skip to content

Instantly share code, notes, and snippets.

@luigircruz
Created December 10, 2022 14:54
Show Gist options
  • Select an option

  • Save luigircruz/181d93a5673448e6a56d913d98708edf to your computer and use it in GitHub Desktop.

Select an option

Save luigircruz/181d93a5673448e6a56d913d98708edf to your computer and use it in GitHub Desktop.
import { useEffect, useState } from 'react';
export default function useDeviceDetect() {
const [isMobile, setMobile] = useState(false);
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 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment