The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update useMediaQuery to set initial state values if window is defined
Tim Wilson975045cff4a1
diff --git a/packages/ui/src/hooks/use-media-query.ts b/packages/ui/src/hooks/use-media-query.tsindex 1740639d90..1e37c571e1 100644--- a/packages/ui/src/hooks/use-media-query.ts+++ b/packages/ui/src/hooks/use-media-query.ts@@ -1,26 +1,34 @@import { useEffect, useState } from "react";+function getDevice(): "mobile" | "tablet" | "desktop" | null {+ if (typeof window === "undefined") return null;++ return window.matchMedia("(max-width: 640px)").matches+ ? "mobile"+ : window.matchMedia("(min-width: 641px) and (max-width: 1024px)").matches+ ? "tablet"+ : "desktop";+}++function getDimensions() {+ if (typeof window === "undefined") return null;++ return { width: window.innerWidth, height: window.innerHeight };+}+export function useMediaQuery() {const [device, setDevice] = useState<"mobile" | "tablet" | "desktop" | null>(- null,+ getDevice(),);const [dimensions, setDimensions] = useState<{width: number;height: number;- } | null>(null);+ } | null>(getDimensions());useEffect(() => {const checkDevice = () => {- if (window.matchMedia("(max-width: 640px)").matches) {- setDevice("mobile");- } else if (- window.matchMedia("(min-width: 641px) and (max-width: 1024px)").matches- ) {- setDevice("tablet");- } else {- setDevice("desktop");- }- setDimensions({ width: window.innerWidth, height: window.innerHeight });+ setDevice(getDevice());+ setDimensions(getDimensions());};// Initial detection