From 2cda4f71b6e2f9d1edf8a9dfd2772ab731f9a847 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Wed, 26 Apr 2023 05:50:49 -0700 Subject: [PATCH] Simplify createInitialState interface (#37069) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37069 changelog: [internal] To create initial C++ state, nothing beside props is needed from `ShadowNodeFragment` and this diff removes it. It makes creation of state easier as we will no longer need to check if props are nullptr. Reviewed By: mdvacca Differential Revision: D45183692 fbshipit-source-id: 81ab8eb3c57f6ff64aaed7c5b395555dce6b60b2 --- .../componentregistry/ComponentDescriptorRegistry.cpp | 3 +-- .../react/renderer/components/image/ImageShadowNode.h | 2 +- .../components/scrollview/ScrollViewShadowNode.cpp | 10 ++-------- .../components/scrollview/ScrollViewShadowNode.h | 2 +- .../AndroidTextInputComponentDescriptor.h | 2 +- .../react/renderer/core/ComponentDescriptor.h | 2 +- .../react/renderer/core/ConcreteComponentDescriptor.h | 4 ++-- .../react/renderer/core/ConcreteShadowNode.h | 2 +- .../react/renderer/element/ComponentBuilder.cpp | 4 ++-- .../ReactCommon/react/renderer/uimanager/UIManager.cpp | 3 +-- 10 files changed, 13 insertions(+), 21 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp b/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp index 2d85ec352734ba..892464bc9cc122 100644 --- a/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp +++ b/packages/react-native/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp @@ -136,8 +136,7 @@ ShadowNode::Shared ComponentDescriptorRegistry::createNode( PropsParserContext{surfaceId, *contextContainer_.get()}, nullptr, RawProps(propsDynamic)); - auto const state = - componentDescriptor.createInitialState(ShadowNodeFragment{props}, family); + auto const state = componentDescriptor.createInitialState(props, family); return componentDescriptor.createShadowNode( { diff --git a/packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.h index 402fa5b93b2317..be233e807b1898 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.h @@ -41,7 +41,7 @@ class ImageShadowNode final : public ConcreteViewShadowNode< void setImageManager(const SharedImageManager &imageManager); static ImageState initialStateData( - ShadowNodeFragment const &fragment, + Props::Shared const &props, ShadowNodeFamilyFragment const &familyFragment, ComponentDescriptor const &componentDescriptor) { auto imageSource = ImageSource{ImageSource::Type::Invalid}; diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.cpp index ec040ce67fd43e..7fe5a502133b0b 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.cpp @@ -49,16 +49,10 @@ void ScrollViewShadowNode::updateScrollContentOffsetIfNeeded() { } ScrollViewState ScrollViewShadowNode::initialStateData( - const ShadowNodeFragment &fragment, + Props::Shared const &props, const ShadowNodeFamilyFragment & /*familyFragment*/, const ComponentDescriptor & /*componentDescriptor*/) { - if (fragment.props != ShadowNodeFragment::propsPlaceholder()) { - auto const &scrollViewProps = - static_cast(*fragment.props); - return {scrollViewProps.contentOffset, {}, 0}; - } else { - return ScrollViewState{}; - } + return {static_cast(*props).contentOffset, {}, 0}; } #pragma mark - LayoutableShadowNode diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.h index 31efaa8fa34454..3d2447fe3d9035 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.h @@ -29,7 +29,7 @@ class ScrollViewShadowNode final : public ConcreteViewShadowNode< using ConcreteViewShadowNode::ConcreteViewShadowNode; static ScrollViewState initialStateData( - ShadowNodeFragment const &fragment, + Props::Shared const &props, ShadowNodeFamilyFragment const &familyFragment, ComponentDescriptor const &componentDescriptor); diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h index 87b22b503bbd27..33f183d58e3738 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputComponentDescriptor.h @@ -34,7 +34,7 @@ class AndroidTextInputComponentDescriptor final } virtual State::Shared createInitialState( - ShadowNodeFragment const &fragment, + Props::Shared const &props, ShadowNodeFamily::Shared const &family) const override { int surfaceId = family->getSurfaceId(); diff --git a/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h index a0d6f137a899d2..c7ced77e41b652 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h @@ -121,7 +121,7 @@ class ComponentDescriptor { * State's data which can be constructed based on initial Props. */ virtual State::Shared createInitialState( - ShadowNodeFragment const &fragment, + Props::Shared const &props, ShadowNodeFamily::Shared const &family) const = 0; /* diff --git a/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h index 7d4d690a6d9e04..6a063b6265016d 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h @@ -151,7 +151,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { }; virtual State::Shared createInitialState( - ShadowNodeFragment const &fragment, + Props::Shared const &props, ShadowNodeFamily::Shared const &family) const override { if (std::is_same::value) { // Default case: Returning `null` for nodes that don't use `State`. @@ -161,7 +161,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { return std::make_shared( std::make_shared( ConcreteShadowNode::initialStateData( - fragment, ShadowNodeFamilyFragment::build(*family), *this)), + props, ShadowNodeFamilyFragment::build(*family), *this)), family); } diff --git a/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h b/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h index f61677645dfdff..b55d30fc4b51c9 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ConcreteShadowNode.h @@ -86,7 +86,7 @@ class ConcreteShadowNode : public BaseShadowNodeT { } static ConcreteStateData initialStateData( - ShadowNodeFragment const &fragment, + Props::Shared const & /*props*/, ShadowNodeFamilyFragment const &familyFragment, ComponentDescriptor const &componentDescriptor) { return {}; diff --git a/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp index 63b2189bea4d20..35973c49a2b3b2 100644 --- a/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp @@ -31,8 +31,8 @@ ShadowNode::Unshared ComponentBuilder::build( elementFragment.tag, elementFragment.surfaceId, nullptr}, nullptr); - auto initialState = componentDescriptor.createInitialState( - ShadowNodeFragment{elementFragment.props}, family); + auto initialState = + componentDescriptor.createInitialState(elementFragment.props, family); auto constShadowNode = componentDescriptor.createShadowNode( ShadowNodeFragment{ diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp index 6434457b9a10d8..2063d246c2cebd 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -80,8 +80,7 @@ ShadowNode::Shared UIManager::createNode( componentDescriptor.createFamily(fragment, std::move(eventTarget)); auto const props = componentDescriptor.cloneProps(propsParserContext, nullptr, rawProps); - auto const state = - componentDescriptor.createInitialState(ShadowNodeFragment{props}, family); + auto const state = componentDescriptor.createInitialState(props, family); auto shadowNode = componentDescriptor.createShadowNode( ShadowNodeFragment{