useIsMobile hook
A client-side hook that tracks viewport width and returns a boolean indicating whether the device is below the mobile breakpoint (768px). Uses window.matchMedia for efficient resize detection without polling.
Hooks
| Hook | Description |
|---|---|
useIsMobile | Returns true when viewport width is below 768px |
useIsMobile
Tracks viewport width using a media query listener and returns whether the current window is mobile-sized.
Signature
function useIsMobile(): boolean
Parameters
None.
Returns
| Field | Type | Description |
|---|---|---|
| (return value) | boolean | true if viewport width is below 768px, false otherwise |
Example
import { useIsMobile } from '@/core/hooks/ui/use-mobile';
function Header() {
const isMobile = useIsMobile();
return isMobile ? <MobileNav /> : <DesktopNav />;
}
Notes
- Initial render returns
false(theundefinedstate is coerced tofalsevia!!isMobile). - The breakpoint constant
MOBILE_BREAKPOINTis set to 768px and is not configurable. - Uses
matchMediachange event listener rather than window resize for better performance.