This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
2021-04-02 02:24:13 +03:00

29 lines
1.1 KiB
JavaScript

import { Dimensions } from 'react-native';
import { useEffect, useState } from 'react';
// Copied from https://github.com/facebook/react-native/blob/8d57691a/Libraries/Utilities/useWindowDimensions.js
// for compatibility with React Native < 0.61.
export default function useWindowDimensions() {
const [dimensions, setDimensions] = useState(() => Dimensions.get('window'));
useEffect(() => {
function handleChange({
window
}) {
if (dimensions.width !== window.width || dimensions.height !== window.height || dimensions.scale !== window.scale || dimensions.fontScale !== window.fontScale) {
setDimensions(window);
}
}
Dimensions.addEventListener('change', handleChange); // We might have missed an update between calling `get` in render and
// `addEventListener` in this handler, so we set it here. If there was
// no change, React will filter out this update as a no-op.
handleChange({
window: Dimensions.get('window')
});
return () => {
Dimensions.removeEventListener('change', handleChange);
};
}, [dimensions]);
return dimensions;
}
//# sourceMappingURL=useWindowDimensions.js.map