Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { useState, useEffect } from 'react';
- /*
- Hook to listen for and return the current
- window inner width and innher height
- ## Usage:
- ```
- const [width, height] = useWindowSize();
- width >= 991 ? 'desktop' : 'mobile'
- ```
- */
- export default function useWindowSize() {
- const [width, updateWidth] = useState(window.innerWidth);
- const [height, updateHeight] = useState(window.innerHeight);
- const handleResize = () => {
- updateWidth(window.innerWidth);
- updateHeight(window.innerHeight);
- };
- useEffect(() => {
- window.addEventListener('resize', handleResize);
- return () => window.removeEventListener('resize', handleResize);
- }, []);
- return [width, height];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement