Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940"use client";import { useState, useEffect } from "react";
export function useIsMobile() { const { width } = useWindowDimensions(); return width < 640 || width === 0;}
export function useIsInitialRender() { let [state, setState] = useState(true); useEffect(() => setState(false), []); return state;}
function getWindowDimensions() { if (typeof window === "undefined") return { width: 0, height: 0 }; const { innerWidth: width, innerHeight: height } = window; return { width, height, };}
export function useWindowDimensions() { const [windowDimensions, setWindowDimensions] = useState( getWindowDimensions(), );
useEffect(() => { function handleResize() { setWindowDimensions(getWindowDimensions()); }
window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []);
return windowDimensions;}