diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 9cc7e768b30..ede0d2c6acb 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -158,13 +158,13 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode( } void YogaLayoutableShadowNode::completeClone( - const ShadowNode& /*sourceShadowNode*/, + const ShadowNode& sourceShadowNode, const ShadowNodeFragment& fragment) { if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode) && // New children means we must always dirty to visit. Otherwise, ask the // Node if the new revision invalidates measurement. (fragment.children || - shouldNewRevisionDirtyMeasurement(*this, fragment))) { + shouldNewRevisionDirtyMeasurement(sourceShadowNode, fragment))) { yogaNode_.setDirty(true); } } diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp index 1e0a06b31da..3a3f4413c3f 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp @@ -828,6 +828,82 @@ using FontScaledTextShadowNode = using FontScaledInputShadowNode = FontScaledShadowNode; +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +static const char SourceComparingComponentName[] = "SourceComparing"; + +/* + * A measurable leaf that keeps its measurement when cloned with the props of + * the revision it is cloned from. + */ +class SourceComparingShadowNode final + : public ConcreteViewShadowNode { + public: + using Base = ConcreteViewShadowNode; + using Base::Base; + + static ShadowNodeTraits BaseTraits() { + auto traits = Base::BaseTraits(); + traits.set(ShadowNodeTraits::Trait::LeafYogaNode); + traits.set(ShadowNodeTraits::Trait::MeasurableYogaNode); + return traits; + } + + protected: + bool shouldNewRevisionDirtyMeasurement( + const ShadowNode& sourceShadowNode, + const ShadowNodeFragment& /*fragment*/) const override { + return sourceShadowNode.getProps() != getProps(); + } +}; + +TEST(ViewTest, completeCloneAsksWhetherToDirtyAgainstTheSourceRevision) { + ComponentDescriptorProviderRegistry providerRegistry{}; + auto registry = providerRegistry.createComponentDescriptorRegistry( + ComponentDescriptorParameters{ + .eventDispatcher = EventDispatcher::Shared{}, + .contextContainer = nullptr, + .flavor = nullptr}); + providerRegistry.add( + concreteComponentDescriptorProvider()); + providerRegistry.add( + concreteComponentDescriptorProvider< + ConcreteComponentDescriptor>()); + auto builder = ComponentBuilder{registry}; + + auto rootShadowNode = std::shared_ptr{}; + auto leafShadowNode = std::shared_ptr{}; + + // clang-format off + auto element = + Element() + .reference(rootShadowNode) + .tag(1) + .children({ + Element() + .reference(leafShadowNode) + .tag(2) + }); + // clang-format on + + builder.build(element); + EXPECT_TRUE(rootShadowNode->layoutIfNeeded()); + ASSERT_TRUE(leafShadowNode->getIsLayoutClean()); + + auto cloneWithSourceProps = leafShadowNode->clone({}); + EXPECT_TRUE( + static_cast(*cloneWithSourceProps) + .getIsLayoutClean()); + + // New non-layout props leave the Yoga style as it was, so only the + // comparison with the source revision can dirty the clone. + auto viewProps = std::make_shared(); + viewProps->opacity = 0.5; + auto cloneWithNewProps = leafShadowNode->clone({.props = viewProps}); + EXPECT_FALSE( + static_cast(*cloneWithNewProps) + .getIsLayoutClean()); +} + // `ModalHostViewShadowNode` needs JNI to create its initial state on Android. #ifndef ANDROID class FontScaleLayoutTest : public ::testing::Test {