현재 실행환경이 모바일인지를 확인하기 위한 Hook입니다.
import { useState, useEffect } from 'react' const useIsMobile = () => { const [isMobile, setIsMobile] = useState(false); useEffect(() => { const checkIsMobile = () => { const userAgent = typeof window.navigator === 'undefined' ? '' : navigator.userAgent; const mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent); setIsMobile(mobile || ('ontouchstart' in window && window.innerWidth <= 1024)); }; checkIsMobile(); window.addEventListener('resize', checkIsMobile); return () => window.removeEventListener('resize', checkIsMobile); }, []); return isMobile; }; export default useIsMobile;