apart from default hooks like useState and useEffect, if you wanna have custom hooks ie functions that react to a change we can do it by following
ex - a hook to check if the device is mobile and rendering based on this
import { useEffect, useState } from "react";
const MOBILE_BREAKPOINT = 768;
export function useIsMobile(breakpoint: number = MOBILE_BREAKPOINT) {
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
const query = window.matchMedia(`(max-width: ${breakpoint - 1}px)`);
const onChange = () => setIsMobile(query.matches);
onChange();
query.addEventListener("change", onChange);
return () => query.removeEventListener("change", onChange);
}, [breakpoint]);
return isMobile;
}