* Custom hook that returns a boolean indicating whether the current device is a mobile device. * It listens for window resize events and updates the value accordingly. * * @returns {boolean} - A boolean value indicating whether the current device is a mobile device.
()
| 10 | * @returns {boolean} - A boolean value indicating whether the current device is a mobile device. |
| 11 | */ |
| 12 | function useIsMobile() { |
| 13 | const [isMobile, setIsMobile] = useState(false); |
| 14 | |
| 15 | useEffect(() => { |
| 16 | function handleResize() { |
| 17 | const isMobileDevice = window.innerWidth < MOBILE_BREAKPOINT; |
| 18 | setIsMobile(isMobileDevice); |
| 19 | } |
| 20 | |
| 21 | // Add event listener |
| 22 | window.addEventListener('resize', handleResize); |
| 23 | |
| 24 | // Call handler right away so state gets updated with initial window size |
| 25 | handleResize(); |
| 26 | |
| 27 | // Remove event listener on cleanup |
| 28 | return () => window.removeEventListener('resize', handleResize); |
| 29 | }, []); // Empty array ensures effect runs only on mount and unmount |
| 30 | |
| 31 | return isMobile; |
| 32 | } |
| 33 | |
| 34 | export default useIsMobile; |
nothing calls this directly
no test coverage detected