From b743a8a35baedab3e5f615e83349811b8dbc6ec0 Mon Sep 17 00:00:00 2001 From: artus9033 Date: Tue, 11 Aug 2026 03:59:23 -0700 Subject: [PATCH] Refresh DeviceInfo constants on resize Summary: Update `RCTDeviceInfo` to refresh cached dimensions when the interface frame, orientation, or content size changes. This provides fresh values from native `getConstants()` after dimension changes. Changelog: [iOS][Fixed] - Refresh `DeviceInfo` constants after interface dimension changes Reviewed By: javache Differential Revision: D113401534 Pulled By: cipolleschi fbshipit-source-id: f8611935f42d1ff2218673405828da7116f62a91 --- .../React/CoreModules/RCTDeviceInfo.mm | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 40b0adf9348..8c1ae6aa40c 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -126,6 +126,12 @@ RCT_EXPORT_MODULE() name:RCTBridgeWillInvalidateModulesNotification object:nil]; + [self invalidateCachedConstants]; +} + +- (void)invalidateCachedConstants +{ + RCTAssertMainQueue(); _constants = @{ @"Dimensions" : [self _exportedDimensions], // Note: @@ -244,20 +250,24 @@ static NSDictionary *RCTExportedDimensions(CGFloat fontScale) - (void)didReceiveNewContentSizeMultiplier { - __weak __typeof(self) weakSelf = self; + [self invalidateCachedConstants]; + NSDictionary *nextInterfaceDimensions = _constants[@"Dimensions"]; + RCTModuleRegistry *moduleRegistry = _moduleRegistry; RCTExecuteOnMainQueue(^{ // Report the event across the bridge. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" [[moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions" - body:[weakSelf _exportedDimensions]]; + body:nextInterfaceDimensions]; #pragma clang diagnostic pop }); } - (void)interfaceOrientationDidChange { + [self invalidateCachedConstants]; + #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST UIWindow *window = RCTKeyWindow(); UIInterfaceOrientation nextOrientation = window.windowScene.interfaceOrientation; @@ -279,8 +289,9 @@ static NSDictionary *RCTExportedDimensions(CGFloat fontScale) if ((isOrientationChanging || isResizingOrChangingToFullscreen) && RCTIsAppActive()) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" + NSDictionary *nextInterfaceDimensions = _constants[@"Dimensions"]; [[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions" - body:[self _exportedDimensions]]; + body:nextInterfaceDimensions]; // We only want to track the current _currentInterfaceOrientation and _isFullscreen only // when it happens and only when it is published. _currentInterfaceOrientation = nextOrientation; @@ -300,7 +311,8 @@ static NSDictionary *RCTExportedDimensions(CGFloat fontScale) - (void)_interfaceFrameDidChange { - NSDictionary *nextInterfaceDimensions = [self _exportedDimensions]; + [self invalidateCachedConstants]; + NSDictionary *nextInterfaceDimensions = _constants[@"Dimensions"]; // update and publish the even only when the app is in active state if (!([nextInterfaceDimensions isEqual:_currentInterfaceDimensions]) && RCTIsAppActive()) {