Skip to content

Commit

Permalink
Only set dimensions if the window attributes change
Browse files Browse the repository at this point in the history
Differential Revision: D19386339

fbshipit-source-id: b7f14c540ceec40cae0c4eb9fc93f2020a6ee16f
  • Loading branch information
cpojer authored and facebook-github-bot committed Jan 14, 2020
1 parent 8219db9 commit 35c6bb9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Libraries/Utilities/useWindowDimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ import {type DisplayMetrics} from './NativeDeviceInfo';
import {useEffect, useState} from 'react';

export default function useWindowDimensions(): DisplayMetrics {
const [dims, setDims] = useState(() => Dimensions.get('window'));
const [dimensions, setDimensions] = useState(() => Dimensions.get('window'));
useEffect(() => {
function handleChange({window}) {
setDims(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.
setDims(Dimensions.get('window'));
handleChange({window: Dimensions.get('window')});
return () => {
Dimensions.removeEventListener('change', handleChange);
};
}, []);
return dims;
}, [dimensions]);
return dimensions;
}

0 comments on commit 35c6bb9

Please sign in to comment.