jaehyuk-jang

useWindowSize() for checking web mobile

1 +export { default as useWindowSize } from './useWindowSize';
1 +import { useLayoutEffect, useState } from 'react';
2 +
3 +export default function useWindowSize() {
4 + const [size, setSize] = useState([0, 0]);
5 +
6 + useLayoutEffect(() => {
7 + function updateSize() {
8 + setSize([window.innerWidth, window.innerHeight]);
9 + }
10 + window.addEventListener('resize', updateSize);
11 + updateSize();
12 + return () => window.removeEventListener('resize', updateSize);
13 + }, []);
14 +
15 + return size;
16 +}