From f1dc061fceb627863c1f8b54e63933e93e64bcd3 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Thu, 23 Nov 2023 14:05:17 -0800 Subject: [PATCH] Use defaultSharedProps in ComponentView init (#41623) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41623 `static` fields require additional code to ensure the field is only ever initialized once. We already have a static "default props" field in the shadow node, so let's reuse that as much as possible. Changelog: [Internal] Reviewed By: rshest Differential Revision: D51547638 fbshipit-source-id: e4957ffb0f9352847b8cd8dc3a010dcfac8be699 --- .../RCTActivityIndicatorViewComponentView.mm | 2 +- .../DebuggingOverlay/RCTDebuggingOverlayComponentView.mm | 4 +--- .../Mounting/ComponentViews/Image/RCTImageComponentView.mm | 2 +- .../InputAccessory/RCTInputAccessoryComponentView.mm | 3 +-- .../RCTLegacyViewManagerInteropComponentView.mm | 3 +-- .../ComponentViews/Modal/RCTModalHostViewComponentView.mm | 3 +-- .../Mounting/ComponentViews/Root/RCTRootComponentView.mm | 3 +-- .../SafeAreaView/RCTSafeAreaViewComponentView.mm | 3 +-- .../ScrollView/RCTPullToRefreshViewComponentView.mm | 3 +-- .../ComponentViews/ScrollView/RCTScrollViewComponentView.mm | 3 +-- .../Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm | 3 +-- .../ComponentViews/Text/RCTParagraphComponentView.mm | 3 +-- .../ComponentViews/TextInput/RCTTextInputComponentView.mm | 5 ++--- .../RCTUnimplementedNativeComponentView.mm | 3 +-- .../UnimplementedView/RCTUnimplementedViewComponentView.mm | 3 +-- .../Mounting/ComponentViews/View/RCTViewComponentView.mm | 3 +-- .../ReactCommon/react/renderer/core/ConcreteShadowNode.h | 2 +- 17 files changed, 18 insertions(+), 33 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ActivityIndicator/RCTActivityIndicatorViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ActivityIndicator/RCTActivityIndicatorViewComponentView.mm index 09b6b70700a27a..4d0c75f282ee5d 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ActivityIndicator/RCTActivityIndicatorViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ActivityIndicator/RCTActivityIndicatorViewComponentView.mm @@ -41,7 +41,7 @@ + (ComponentDescriptorProvider)componentDescriptorProvider - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); + const auto &defaultProps = ActivityIndicatorViewShadowNode::defaultSharedProps(); _props = defaultProps; _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:self.bounds]; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/DebuggingOverlay/RCTDebuggingOverlayComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/DebuggingOverlay/RCTDebuggingOverlayComponentView.mm index 31d1dcb5f77962..c9ac90286f9c4c 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/DebuggingOverlay/RCTDebuggingOverlayComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/DebuggingOverlay/RCTDebuggingOverlayComponentView.mm @@ -27,9 +27,7 @@ @implementation RCTDebuggingOverlayComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; - + _props = DebuggingOverlayShadowNode::defaultSharedProps(); _overlay = [[RCTDebuggingOverlay alloc] initWithFrame:self.bounds]; self.contentView = _overlay; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm index e48d5470179e4f..2de560d78f74b3 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Image/RCTImageComponentView.mm @@ -28,7 +28,7 @@ @implementation RCTImageComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); + const auto &defaultProps = ImageShadowNode::defaultSharedProps(); _props = defaultProps; _imageView = [RCTUIImageViewAnimated new]; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/InputAccessory/RCTInputAccessoryComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/InputAccessory/RCTInputAccessoryComponentView.mm index 454c3ff1ac279d..6ace11ce5ab012 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/InputAccessory/RCTInputAccessoryComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/InputAccessory/RCTInputAccessoryComponentView.mm @@ -50,8 +50,7 @@ @implementation RCTInputAccessoryComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = InputAccessoryShadowNode::defaultSharedProps(); _contentView = [RCTInputAccessoryContentView new]; _touchHandler = [RCTSurfaceTouchHandler new]; [_touchHandler attachToView:_contentView]; 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 42fa691dfebd55..86aee3223d7659 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/LegacyViewManagerInterop/RCTLegacyViewManagerInteropComponentView.mm @@ -31,8 +31,7 @@ @implementation RCTLegacyViewManagerInteropComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = LegacyViewManagerInteropShadowNode::defaultSharedProps(); _viewsToBeMounted = [NSMutableArray new]; _viewsToBeUnmounted = [NSMutableArray new]; _hasInvokedForwardingWarning = NO; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm index 445a5c79a5afaf..e20eee608a01c6 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm @@ -110,8 +110,7 @@ @implementation RCTModalHostViewComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = ModalHostViewShadowNode::defaultSharedProps(); _shouldAnimatePresentation = YES; _isPresented = NO; 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 4efa8b34b4a7a0..eb81a151c3fc41 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Root/RCTRootComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Root/RCTRootComponentView.mm @@ -18,8 +18,7 @@ @implementation RCTRootComponentView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = RootShadowNode::defaultSharedProps(); } return self; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm index 4c9060f1a622fd..709e378ad49d83 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm @@ -22,8 +22,7 @@ @implementation RCTSafeAreaViewComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = SafeAreaViewShadowNode::defaultSharedProps(); } return self; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTPullToRefreshViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTPullToRefreshViewComponentView.mm index 231777cd48950a..64b09228a397e5 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTPullToRefreshViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTPullToRefreshViewComponentView.mm @@ -36,8 +36,7 @@ - (instancetype)initWithFrame:(CGRect)frame // The pull-to-refresh view is not a subview of this view. self.hidden = YES; - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = PullToRefreshViewShadowNode::defaultSharedProps(); _refreshControl = [UIRefreshControl new]; [_refreshControl addTarget:self diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index 504b0ef147ae0b..8272db8c85082c 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -118,8 +118,7 @@ + (RCTScrollViewComponentView *_Nullable)findScrollViewComponentViewForView:(UIV - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = ScrollViewShadowNode::defaultSharedProps(); _scrollView = [[RCTEnhancedScrollView alloc] initWithFrame:self.bounds]; _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm index d4ed3860e474c7..941ad9b7f0978e 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm @@ -29,8 +29,7 @@ @implementation RCTSwitchComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = SwitchShadowNode::defaultSharedProps(); _switchView = [[UISwitch alloc] initWithFrame:self.bounds]; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm index fce427848a9c11..0787ef71cafe8e 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm @@ -40,8 +40,7 @@ @implementation RCTParagraphComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = ParagraphShadowNode::defaultSharedProps(); self.opaque = NO; self.contentMode = UIViewContentModeRedraw; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index e7b69bff2f9e68..44e74da5cea06f 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -62,11 +62,10 @@ @implementation RCTTextInputComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); + const auto &defaultProps = TextInputShadowNode::defaultSharedProps(); _props = defaultProps; - auto &props = *defaultProps; - _backedTextInputView = props.traits.multiline ? [RCTUITextView new] : [RCTUITextField new]; + _backedTextInputView = defaultProps->traits.multiline ? [RCTUITextView new] : [RCTUITextField new]; _backedTextInputView.textInputDelegate = self; _ignoreNextTextInputCall = NO; _comingFromJS = NO; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedComponent/RCTUnimplementedNativeComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedComponent/RCTUnimplementedNativeComponentView.mm index fc39e2592de4d0..fe4c9f6a65b142 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedComponent/RCTUnimplementedNativeComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedComponent/RCTUnimplementedNativeComponentView.mm @@ -20,8 +20,7 @@ @implementation RCTUnimplementedNativeComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = UnimplementedNativeViewShadowNode::defaultSharedProps(); CGRect bounds = self.bounds; _label = [[UILabel alloc] initWithFrame:bounds]; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedView/RCTUnimplementedViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedView/RCTUnimplementedViewComponentView.mm index 6a9efe577fb045..b5185677875a13 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedView/RCTUnimplementedViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/UnimplementedView/RCTUnimplementedViewComponentView.mm @@ -27,8 +27,7 @@ @implementation RCTUnimplementedViewComponentView { - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = UnimplementedViewShadowNode::defaultSharedProps(); _label = [[UILabel alloc] initWithFrame:self.bounds]; _label.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.3]; diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 9fa696f7a49a72..6751018c889162 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -46,8 +46,7 @@ + (void)load - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); - _props = defaultProps; + _props = ViewShadowNode::defaultSharedProps(); _reactSubviews = [NSMutableArray new]; self.multipleTouchEnabled = YES; } diff --git a/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h b/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h index e2a793b507185e..ab2ed6cf4da621 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h @@ -85,7 +85,7 @@ class ConcreteShadowNode : public BaseShadowNodeT { rawProps); } - static SharedConcreteProps defaultSharedProps() { + static const SharedConcreteProps& defaultSharedProps() { static const SharedConcreteProps defaultSharedProps = std::make_shared(); return defaultSharedProps;