From febe9afb84f41374f57629d3ee92699d10bf7115 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 31 Jan 2024 02:04:09 -0800 Subject: [PATCH] Fix crash in the interop layer when components emits events as soon as are created (#42743) Summary: When working on Mobile Home, we found a component (RNCSafeAreaView) that was going through the interop layer. The component eits an event as soon as its content view changes, but this is too early: the block that emits the event is still nil at that point in time and that makes the app crash. There might be other components with similarbehavior, therefore, we are fixing it at the interop layer, setting the props immediately after the component is created. ## Changelog: [iOS][Fixed] - Immediately set props of Components that goes through the interop layer Differential Revision: D53230471 --- ...RCTLegacyViewManagerInteropComponentView.mm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm index b29cffd89bed13..d25c98f6fdc6a3 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm @@ -210,7 +210,7 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared & - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask { [super finalizeUpdates:updateMask]; - + BOOL propsUpdated = NO; if (!_adapter) { _adapter = [[RCTLegacyViewManagerInteropCoordinatorAdapter alloc] initWithCoordinator:[self _coordinator] reactTag:self.tag]; @@ -223,6 +223,14 @@ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask eventEmitter.dispatchEvent(eventName, event); } }; + // Set props immediately. This is required to set the initial state of the view. + // In the case where some events are fired in relationship of a change in the frame + // or layout of the view, they will fire as soon as the contentView is set and if the + // event Block is nil, the app will crash. + if (!propsUpdated) { + [self _setPropsWithUpdateMask:updateMask]; + propsUpdated = YES; + } self.contentView = _adapter.paperView; } @@ -247,6 +255,14 @@ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask [_adapter.paperView didUpdateReactSubviews]; + if (!propsUpdated) { + [self _setPropsWithUpdateMask:updateMask]; + propsUpdated = YES; + } +} + +- (void)_setPropsWithUpdateMask:(RNComponentViewUpdateMask)updateMask +{ if (updateMask & RNComponentViewUpdateMaskProps) { const auto &newProps = static_cast(*_props); [_adapter setProps:newProps.otherProps];