Rehooks
Content

useDevice

A hook that detects the device type and returns a boolean value for each device type.

useDevice

Usage

Component.tsx
import { useDevice } from "~/hooks/useDevice";
 
function Component() {
  const { isMobile, isDesktop, isTablet, DesktopView, MobileView, TabletView } =
    useDevice();
 
  return (
    <div>
      <div>
        {isMobile && <div>Mobile View</div>}
        {isDesktop && <div>Desktop View</div>}
        {isTablet && <div>Tablet View</div>}
      </div>
      <button onClick={DesktopView}>
        {isDesktop ? "Hide" : "Show"} Desktop View
      </button>
      <button onClick={MobileView}>
        {isMobile ? "Hide" : "Show"} Mobile View
      </button>
      <button onClick={TabletView}>
        {isTablet ? "Hide" : "Show"} Tablet View
      </button>
    </div>
  );
}

API

useDevice

function useDevice(): {
  isMobile: boolean;
  isDesktop: boolean;
  isTablet: boolean;
  DesktopView: () => void;
  MobileView: () => void;
  TabletView: () => void;
};

Returns a boolean value for each device type.

Returns

NameTypeDescription
isMobilebooleanA boolean value indicating whether the device is a mobile device.
isDesktopbooleanA boolean value indicating whether the device is a desktop device.
isTabletbooleanA boolean value indicating whether the device is a tablet device.
DesktopView() => voidA function that toggles the visibility of the desktop view.
MobileView() => voidA function that toggles the visibility of the mobile view.
TabletView() => voidA function that toggles the visibility of the tablet view.

On this page