a tool for shared writing and social publishing
1import { useState, useEffect } from "react"; 2 3export function useIsMobile() { 4 const { width } = useWindowDimensions(); 5 return width < 640 || width === 0; 6} 7 8export function useIsInitialRender() { 9 let [state, setState] = useState(true); 10 useEffect(() => setState(false), []); 11 return state; 12} 13 14function getWindowDimensions() { 15 if (typeof window === "undefined") return { width: 0, height: 0 }; 16 const { innerWidth: width, innerHeight: height } = window; 17 return { 18 width, 19 height, 20 }; 21} 22 23export function useWindowDimensions() { 24 const [windowDimensions, setWindowDimensions] = useState( 25 getWindowDimensions(), 26 ); 27 28 useEffect(() => { 29 function handleResize() { 30 setWindowDimensions(getWindowDimensions()); 31 } 32 33 window.addEventListener("resize", handleResize); 34 return () => window.removeEventListener("resize", handleResize); 35 }, []); 36 37 return windowDimensions; 38}