feat(front-end): add hook to access innerWidth whenever the viewport is resized

main
Josue 4 years ago
parent dc34e6e3b6
commit f51f6acb4d
  1. 19
      striker-ui/hooks/useWindowDimenions.ts

@ -0,0 +1,19 @@
import { useEffect, useState } from 'react';
const useWindowDimensions = (): number | undefined => {
const [windowDimensions, setWindowDimensions] = useState<number | undefined>(
undefined,
);
useEffect(() => {
const handleResize = (): void => {
setWindowDimensions(window.innerWidth);
};
handleResize();
window.addEventListener('resize', handleResize);
return (): void => window.removeEventListener('resize', handleResize);
}, []); // Empty array ensures that effect is only run on mount
return windowDimensions;
};
export default useWindowDimensions;
Loading…
Cancel
Save