Hooks
No hooks found in any category.
useIsMobile
sensorsInstallation
npx usehooks-cli@latest add use-is-mobile
Description
A React hook for detecting mobile devices using media queries and user agent detection. Provides real-time updates when the device orientation or window size changes.
Return Type
UseIsMobileReturn
Property | Type | Description |
---|---|---|
isMobile | boolean | Whether the current device is detected as mobile |
isLoading | boolean | Whether the hook is still determining the device type |
Examples
Basic Mobile Detection
Simple mobile device detection
1const { isMobile, isLoading } = useIsMobile();
2
3if (isLoading) {
4 return <div>Detecting device...</div>;
5}
6
7return (
8 <div>
9 <p>Device type: {isMobile ? 'Mobile' : 'Desktop'}</p>
10 {isMobile ? (
11 <MobileNavigation />
12 ) : (
13 <DesktopNavigation />
14 )}
15 </div>
16);
Responsive Component
Conditionally render components based on device type
1const { isMobile } = useIsMobile();
2
3return (
4 <div className={isMobile ? 'mobile-layout' : 'desktop-layout'}>
5 {isMobile ? (
6 <TouchFriendlyButton />
7 ) : (
8 <HoverButton />
9 )}
10 </div>
11);
Dependencies
react
Notes
- •Uses media query (max-width: 768px) as primary detection method
- •Falls back to user agent detection for additional accuracy
- •Automatically updates when window is resized or orientation changes
- •Provides loading state during initial detection
Implementation
1'use client';
2
3import { useState, useEffect } from "react";
4
5interface UseIsMobileReturn {
6 isMobile: boolean;
7 isLoading: boolean;
8}
9
10export const useIsMobile = (): UseIsMobileReturn => {
11 const [isMobile, setIsMobile] = useState(false);
12 const [isLoading, setIsLoading] = useState(true);
13
14 useEffect(() => {
15 const checkIsMobile = () => {
16 // Check using media query
17 const mediaQuery = window.matchMedia("(max-width: 768px)");
18
19 // Check using user agent (additional detection)
20 const userAgent = navigator.userAgent.toLowerCase();
21 const mobileKeywords = [
22 'android', 'webos', 'iphone', 'ipad', 'ipod',
23 'blackberry', 'windows phone', 'mobile'
24 ];
25
26 const isMobileUA = mobileKeywords.some(keyword =>
27 userAgent.includes(keyword)
28 );
29
30 // Combine both checks - prioritize media query but consider user agent
31 const isMobileDevice = mediaQuery.matches ||
32 (isMobileUA && window.innerWidth <= 768);
33
34 setIsMobile(isMobileDevice);
35 setIsLoading(false);
36 };
37
38 // Initial check
39 checkIsMobile();
40
41 // Listen for media query changes
42 const mediaQuery = window.matchMedia("(max-width: 768px)");
43 const handleChange = () => checkIsMobile();
44
45 if (mediaQuery.addEventListener) {
46 mediaQuery.addEventListener('change', handleChange);
47 } else {
48 // Fallback for older browsers
49 mediaQuery.addListener(handleChange);
50 }
51
52 // Listen for window resize
53 window.addEventListener('resize', checkIsMobile);
54
55 return () => {
56 if (mediaQuery.removeEventListener) {
57 mediaQuery.removeEventListener('change', handleChange);
58 } else {
59 mediaQuery.removeListener(handleChange);
60 }
61 window.removeEventListener('resize', checkIsMobile);
62 };
63 }, []);
64
65 return {
66 isMobile,
67 isLoading,
68 };
69};
70
71export default useIsMobile;