GT2/Ejectable/node_modules/@react-navigation/bottom-tabs/lib/module/utils/useWindowDimensions.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-08-16 00:14:59 +00:00
import * as React from 'react';
import { Dimensions } from 'react-native'; // This is similar to the new useWindowDimensions hook in react-native
// However, we have a custom implementation to support older RN versions
export default function useWindowDimensions() {
const [dimensions, setDimensions] = React.useState(() => {
// `height` and `width` maybe undefined during SSR, so we initialize them
const {
height = 0,
width = 0
} = Dimensions.get('window');
return {
height,
width
};
});
React.useEffect(() => {
const onChange = ({
window
}) => {
const {
width,
height
} = window;
setDimensions(d => {
if (width === d.width && height === d.height) {
return d;
}
return {
width,
height
};
});
}; // We might have missed an update before the listener was added
// So make sure to update the dimensions
onChange({
window: Dimensions.get('window')
});
Dimensions.addEventListener('change', onChange);
return () => Dimensions.removeEventListener('change', onChange);
}, []);
return dimensions;
}
//# sourceMappingURL=useWindowDimensions.js.map