From d6c90cf7ed80caf0fc4eb2541f3dcadc3833dd37 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Mon, 8 Jul 2024 11:07:29 -0700 Subject: [PATCH] Make sure to pass the BridgeProxy to view managers in the interop layer (#45329) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45329 Thanks to [#45232](https://github.com/facebook/react-native/issues/45232) we found a bug in the interop layer, where we were not passing the BridgeProxy in bridgeless mode to the view managers. This Change should fix that issue. ## Changelog: [iOS][Fixed] - Make sure to pass the RCTBridgeProxy to ViewManagers Reviewed By: dmytrorykun Differential Revision: D59468292 fbshipit-source-id: 00666be21385a735878eb567c4b8a0986c609c5f --- .../react-native/React/Views/RCTComponentData.m | 14 +++++++++++--- .../LegacyViewManagerInteropComponentDescriptor.mm | 7 ++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/react-native/React/Views/RCTComponentData.m b/packages/react-native/React/Views/RCTComponentData.m index 3952d177e66063..f743aa88b7bc7c 100644 --- a/packages/react-native/React/Views/RCTComponentData.m +++ b/packages/react-native/React/Views/RCTComponentData.m @@ -61,12 +61,20 @@ - (instancetype)initWithManagerClass:(Class)managerClass return self; } +- (BOOL)isBridgeMode +{ + // If we are in bridge mode, the bridge is RCTBridge + // If we are bridgeless, the bridge is RCTBridgeProxy + return [_bridge isKindOfClass:[RCTBridge class]]; +} + - (RCTViewManager *)manager { - if (!_manager && _bridge) { + if (!_manager && [self isBridgeMode]) { _manager = [_bridge moduleForClass:_managerClass]; } else if (!_manager && !_bridgelessViewManager) { _bridgelessViewManager = [_managerClass new]; + _bridgelessViewManager.bridge = _bridge; [[NSNotificationCenter defaultCenter] postNotificationName:RCTDidInitializeModuleNotification object:nil userInfo:@{@"module" : _bridgelessViewManager}]; @@ -265,8 +273,8 @@ - (RCTPropBlock)createPropBlock:(NSString *)name isShadowView:(BOOL)isShadowView type == NSSelectorFromString(@"RCTDirectEventBlock:") || type == NSSelectorFromString(@"RCTCapturingEventBlock:")) { // Special case for event handlers - setterBlock = - createEventSetter(name, setter, self.eventInterceptor, _bridge ? _bridge.eventDispatcher : _eventDispatcher); + setterBlock = createEventSetter( + name, setter, self.eventInterceptor, [self isBridgeMode] ? _bridge.eventDispatcher : _eventDispatcher); } else { // Ordinary property handlers NSMethodSignature *typeSignature = [[RCTConvert class] methodSignatureForSelector:type]; diff --git a/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.mm b/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.mm index f7c895ee2aa5ad..eb8fa9a86f2161 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.mm +++ b/packages/react-native/ReactCommon/react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.mm @@ -125,9 +125,10 @@ static Class getViewManagerClass(const std::string &componentName, RCTBridge *br bridgeModuleDecorator = unwrapManagedObject(optionalModuleDecorator.value()); } - RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:viewManagerClass - bridge:bridge - eventDispatcher:eventDispatcher]; + RCTComponentData *componentData = + [[RCTComponentData alloc] initWithManagerClass:viewManagerClass + bridge:bridge != nil ? bridge : (RCTBridge *)bridgeProxy + eventDispatcher:eventDispatcher]; return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc] initWithComponentData:componentData bridge:bridge