From c7d9571d6acbbeea1689934d6036864238eff656 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 4 Apr 2024 05:52:53 -0700 Subject: [PATCH] only trigger RCTContentDidAppearNotification when content appears (#43823) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43823 changelog: [internal] Notification RCTContentDidAppearNotification was posted too early in RCTSurfaceHostingProxyRootView, which does not know when views are mounted. It was also posted if no views were mounted, leading to inconsistent behaviour between Paper and Fabric. The implementation is aligned with Paper: https://github.com/facebook/react-native/blob/main/packages/react-native/React/Base/RCTRootContentView.m#L45-L55 Reviewed By: cipolleschi Differential Revision: D55640654 fbshipit-source-id: 2d7bc5afb6ba1c1e8db529ee11eac2bae2d936d6 --- .../RCTSurfaceHostingProxyRootView.mm | 3 --- .../Root/RCTRootComponentView.mm | 23 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm b/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm index 670c0229960..39377fe8b6f 100644 --- a/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm +++ b/packages/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm @@ -125,9 +125,6 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder) [super surface:surface didChangeStage:stage]; if (RCTSurfaceStageIsRunning(stage)) { [_bridge.performanceLogger markStopForTag:RCTPLTTI]; - dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:RCTContentDidAppearNotification object:self]; - }); } } diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Root/RCTRootComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Root/RCTRootComponentView.mm index eb81a151c3f..c7d0a525a9a 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Root/RCTRootComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Root/RCTRootComponentView.mm @@ -7,18 +7,22 @@ #import "RCTRootComponentView.h" +#import #import #import #import "RCTConversions.h" using namespace facebook::react; -@implementation RCTRootComponentView +@implementation RCTRootComponentView { + BOOL _contentHasAppeared; +} - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _props = RootShadowNode::defaultSharedProps(); + _contentHasAppeared = NO; } return self; @@ -26,6 +30,23 @@ using namespace facebook::react; #pragma mark - RCTComponentViewProtocol +- (void)prepareForRecycle +{ + [super prepareForRecycle]; + _contentHasAppeared = NO; +} + +- (void)mountChildComponentView:(UIView *)childComponentView index:(NSInteger)index +{ + [super mountChildComponentView:childComponentView index:index]; + if (!self->_contentHasAppeared) { + self->_contentHasAppeared = YES; + dispatch_async(dispatch_get_main_queue(), ^{ + [[NSNotificationCenter defaultCenter] postNotificationName:RCTContentDidAppearNotification object:self]; + }); + } +} + + (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider();