Created
October 27, 2020 07:13
-
-
Save mikaelbr/f2d3bee8050580ac6d003fcd2136077c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ScreenDimensions = { | |
height: number; | |
width: number; | |
} | |
function useScreenDimension(): ScreenDimensions { | |
const [dimensions, setDimensions] = useState<ScreenDimensions>(() => | |
Dimensions.get('screen'), | |
); | |
useEffect(() => { | |
const handleDimensionsChange = ({screen}: {screen: ScaledSize}): void => { | |
const {height, width} = screen; | |
if (dimensions.height !== height || dimensions.width !== width) { | |
setDimensions({height, width}); | |
} | |
}; | |
Dimensions.addEventListener('change', handleDimensionsChange); | |
return () => | |
Dimensions.removeEventListener('change', handleDimensionsChange); | |
}, [Dimensions]); | |
return dimensions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment