From c5357795bbb2e21ed8c8f125fc2844fa1c460f66 Mon Sep 17 00:00:00 2001 From: gunshiz Date: Sun, 16 Aug 2026 01:02:58 +0700 Subject: [PATCH] fix: modernize mobile breakpoint subscription --- hooks/use-mobile.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/hooks/use-mobile.ts b/hooks/use-mobile.ts index 2b0fe1d..841e164 100644 --- a/hooks/use-mobile.ts +++ b/hooks/use-mobile.ts @@ -2,18 +2,18 @@ import * as React from "react" const MOBILE_BREAKPOINT = 768 -export function useIsMobile() { - const [isMobile, setIsMobile] = React.useState(undefined) - - React.useEffect(() => { - const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`) - const onChange = () => { - setIsMobile(window.innerWidth < MOBILE_BREAKPOINT) - } - mql.addEventListener("change", onChange) - setIsMobile(window.innerWidth < MOBILE_BREAKPOINT) - return () => mql.removeEventListener("change", onChange) - }, []) - - return !!isMobile +function subscribe(callback: () => void) { + const mediaQuery = window.matchMedia( + `(max-width: ${MOBILE_BREAKPOINT - 1}px)` + ) + mediaQuery.addEventListener("change", callback) + return () => mediaQuery.removeEventListener("change", callback) +} + +function getSnapshot() { + return window.innerWidth < MOBILE_BREAKPOINT +} + +export function useIsMobile() { + return React.useSyncExternalStore(subscribe, getSnapshot, () => false) }