Files
cunkebao_v3/Cunkebao/hooks/use-mobile.tsx

29 lines
599 B
TypeScript
Raw Normal View History

"use client"
2025-03-29 16:50:39 +08:00
import { useState, useEffect } from "react"
2025-03-29 16:50:39 +08:00
const MOBILE_MAX_WIDTH = 768 // Adjust as needed
2025-03-29 16:50:39 +08:00
export function useMobile() {
const [isMobile, setIsMobile] = useState(false)
useEffect(() => {
const checkScreenSize = () => {
setIsMobile(window.innerWidth <= MOBILE_MAX_WIDTH)
}
// Initial check
checkScreenSize()
// Subscribe to the window resize event
window.addEventListener("resize", checkScreenSize)
// Unsubscribe on unmount
return () => {
window.removeEventListener("resize", checkScreenSize)
2025-03-29 16:50:39 +08:00
}
}, [])
return isMobile
2025-03-29 16:50:39 +08:00
}