Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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