Skip to content

Commit 07e9679

Browse files
Pass the source revision to shouldNewRevisionDirtyMeasurement
`YogaLayoutableShadowNode::completeClone()` passed the clone itself as the `sourceShadowNode` argument, so an override comparing the new revision with its source compared the clone with itself and never dirtied measurement. Signed-off-by: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com>
1 parent 018bd47 commit 07e9679

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode(
158158
}
159159

160160
void YogaLayoutableShadowNode::completeClone(
161-
const ShadowNode& /*sourceShadowNode*/,
161+
const ShadowNode& sourceShadowNode,
162162
const ShadowNodeFragment& fragment) {
163163
if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode) &&
164164
// New children means we must always dirty to visit. Otherwise, ask the
165165
// Node if the new revision invalidates measurement.
166166
(fragment.children ||
167-
shouldNewRevisionDirtyMeasurement(*this, fragment))) {
167+
shouldNewRevisionDirtyMeasurement(sourceShadowNode, fragment))) {
168168
yogaNode_.setDirty(true);
169169
}
170170
}

‎packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,82 @@ using FontScaledTextShadowNode =
828828
using FontScaledInputShadowNode =
829829
FontScaledShadowNode<FontScaledInputComponentName, false>;
830830

831+
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
832+
static const char SourceComparingComponentName[] = "SourceComparing";
833+
834+
/*
835+
* A measurable leaf that keeps its measurement when cloned with the props of
836+
* the revision it is cloned from.
837+
*/
838+
class SourceComparingShadowNode final
839+
: public ConcreteViewShadowNode<SourceComparingComponentName> {
840+
public:
841+
using Base = ConcreteViewShadowNode<SourceComparingComponentName>;
842+
using Base::Base;
843+
844+
static ShadowNodeTraits BaseTraits() {
845+
auto traits = Base::BaseTraits();
846+
traits.set(ShadowNodeTraits::Trait::LeafYogaNode);
847+
traits.set(ShadowNodeTraits::Trait::MeasurableYogaNode);
848+
return traits;
849+
}
850+
851+
protected:
852+
bool shouldNewRevisionDirtyMeasurement(
853+
const ShadowNode& sourceShadowNode,
854+
const ShadowNodeFragment& /*fragment*/) const override {
855+
return sourceShadowNode.getProps() != getProps();
856+
}
857+
};
858+
859+
TEST(ViewTest, completeCloneAsksWhetherToDirtyAgainstTheSourceRevision) {
860+
ComponentDescriptorProviderRegistry providerRegistry{};
861+
auto registry = providerRegistry.createComponentDescriptorRegistry(
862+
ComponentDescriptorParameters{
863+
.eventDispatcher = EventDispatcher::Shared{},
864+
.contextContainer = nullptr,
865+
.flavor = nullptr});
866+
providerRegistry.add(
867+
concreteComponentDescriptorProvider<RootComponentDescriptor>());
868+
providerRegistry.add(
869+
concreteComponentDescriptorProvider<
870+
ConcreteComponentDescriptor<SourceComparingShadowNode>>());
871+
auto builder = ComponentBuilder{registry};
872+
873+
auto rootShadowNode = std::shared_ptr<RootShadowNode>{};
874+
auto leafShadowNode = std::shared_ptr<SourceComparingShadowNode>{};
875+
876+
// clang-format off
877+
auto element =
878+
Element<RootShadowNode>()
879+
.reference(rootShadowNode)
880+
.tag(1)
881+
.children({
882+
Element<SourceComparingShadowNode>()
883+
.reference(leafShadowNode)
884+
.tag(2)
885+
});
886+
// clang-format on
887+
888+
builder.build(element);
889+
EXPECT_TRUE(rootShadowNode->layoutIfNeeded());
890+
ASSERT_TRUE(leafShadowNode->getIsLayoutClean());
891+
892+
auto cloneWithSourceProps = leafShadowNode->clone({});
893+
EXPECT_TRUE(
894+
static_cast<const YogaLayoutableShadowNode&>(*cloneWithSourceProps)
895+
.getIsLayoutClean());
896+
897+
// New non-layout props leave the Yoga style as it was, so only the
898+
// comparison with the source revision can dirty the clone.
899+
auto viewProps = std::make_shared<ViewProps>();
900+
viewProps->opacity = 0.5;
901+
auto cloneWithNewProps = leafShadowNode->clone({.props = viewProps});
902+
EXPECT_FALSE(
903+
static_cast<const YogaLayoutableShadowNode&>(*cloneWithNewProps)
904+
.getIsLayoutClean());
905+
}
906+
831907
// `ModalHostViewShadowNode` needs JNI to create its initial state on Android.
832908
#ifndef ANDROID
833909
class FontScaleLayoutTest : public ::testing::Test {

0 commit comments

Comments
 (0)