Skip to main content

useIsMobile

The useIsMobile hook detects whether the current device is mobile by checking if the viewport width is less than 768px. It listens to window resize events and updates automatically.

Installation

Import

Usage

Returns

boolean
A boolean value indicating whether the viewport width is less than 768px. Returns false during server-side rendering and updates after mounting on the client.

Type Definition

Breakpoint

The hook uses a mobile breakpoint of 768px:
  • isMobile = true when viewport width is < 768px
  • isMobile = false when viewport width is >= 768px

Examples

Responsive Navigation

Conditional Component Rendering

Different Layouts

Touch vs Click Events

Responsive Image Loading

Responsive Table

Common Patterns

Combining with CSS Classes

Conditional Hook Calls

Performance Optimization

Notes

  • The hook returns false during server-side rendering
  • The initial state is undefined, which is coerced to false by the !! operator
  • The hook automatically updates when the window is resized
  • Event listeners are properly cleaned up when the component unmounts
  • The 768px breakpoint is commonly used as the mobile/tablet boundary
  • For more complex responsive logic, consider using useMediaQuery hook instead
  • The hook uses window.matchMedia for efficient media query matching
  • This hook is ideal for simple mobile/desktop detection; for more granular control, use custom media queries