From 35c6bb9ac0fc452428e85fee72affb4fc29f500c Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Mon, 13 Jan 2020 23:18:22 -0800 Subject: [PATCH] Only set dimensions if the window attributes change Differential Revision: D19386339 fbshipit-source-id: b7f14c540ceec40cae0c4eb9fc93f2020a6ee16f --- Libraries/Utilities/useWindowDimensions.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Libraries/Utilities/useWindowDimensions.js b/Libraries/Utilities/useWindowDimensions.js index 3de619a5eab877..f298649c943ccc 100644 --- a/Libraries/Utilities/useWindowDimensions.js +++ b/Libraries/Utilities/useWindowDimensions.js @@ -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; }