From 63243ca64ae9a1ac4f09a7b3079afbb062c361a8 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Thu, 26 Nov 2026 12:10:08 +0200 Subject: [PATCH 1/2] Fix out-of-order children after a view drops its container view (iOS) A recycled RCTViewComponentView kept the clipping container of its previous owner, and moving children back out of a container appended them after the view's own box shadow layers. insertSubview:atIndex: counts sublayers, so later mounts landed one index too low, which asserts on unmount in debug builds. Drop the container in prepareForRecycle and move children back below the view's own layers. --- .../View/RCTViewComponentView.mm | 13 +- .../RCTViewComponentViewContainerTests.mm | 161 ++++++++++++++++++ .../RCTViewComponentViewUnmountTests.mm | 19 +-- 3 files changed, 180 insertions(+), 13 deletions(-) create mode 100644 packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm 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 8d1295d8db36..e0abe9312742 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -891,6 +891,12 @@ - (void)prepareForRecycle _filterLayer = nil; [self clearExistingBackgroundImageLayers]; + // Drop the clipping container of the previous owner. Fabric mounts a view's + // children before it applies that view's props, so a container left in place + // would receive the next owner's children. + _useCustomContainerView = NO; + [self currentContainerView]; + [self _setLastSentSafeAreaInsets:nil]; _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil; _eventEmitter.reset(); @@ -1149,8 +1155,13 @@ - (UIView *)currentContainerView effectiveContentView = _containerView; } else { if (_containerView) { + // Put the children back below the layers this view draws itself (box + // shadows, outlines), where they sit when no container is used. + // `insertSubview:atIndex:` counts sublayers, so appending them after + // those layers would shift every later mount by one. + NSUInteger index = 0; for (UIView *subview in _containerView.subviews) { - [effectiveContentView addSubview:subview]; + [effectiveContentView insertSubview:subview atIndex:index++]; } effectiveContentView.clipsToBounds = _containerView.clipsToBounds; effectiveContentView.layer.mask = _containerView.layer.mask; diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm new file mode 100644 index 000000000000..2e4839b05b5a --- /dev/null +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm @@ -0,0 +1,161 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import +#import +#import +#import +#import + +using namespace facebook::react; + +// A view that clips and has a box shadow draws the shadow outside its bounds, +// so RCTViewComponentView moves its children into a separate clipping +// container view. +static Props::Shared makeProps(bool clips, bool hasBoxShadow) +{ + auto props = std::make_shared(); + if (clips) { + props->yogaStyle.setOverflow(facebook::yoga::Overflow::Hidden); + } + if (hasBoxShadow) { + BoxShadow shadow; + shadow.offsetY = 2; + shadow.blurRadius = 6; + shadow.color = colorFromRGBA(0, 0, 0, 102); + props->boxShadow = {shadow}; + } + return props; +} + +static void applyProps(RCTViewComponentView *view, const Props::Shared &props) +{ + [view updateProps:props oldProps:view.props]; + LayoutMetrics layoutMetrics; + layoutMetrics.frame.size = {100, 40}; + [view updateLayoutMetrics:layoutMetrics oldLayoutMetrics:EmptyLayoutMetrics]; + [view finalizeUpdates:RNComponentViewUpdateMaskAll]; +} + +// Unmounts `child` and returns the message of any RCTAssert it raised, or nil. +static NSString * +unmountAndReturnAssertMessage(RCTViewComponentView *parent, UIView *child, NSInteger index) +{ + __block NSString *message = nil; + RCTPerformBlockWithAssertFunction( + ^{ + @try { + [parent unmountChildComponentView:child index:index]; + } @catch (NSException *exception) { + message = message ?: exception.reason; + } + }, + ^(NSString *condition, NSString *fileName, NSNumber *lineNumber, NSString *function, NSString *assertMessage) { + message = assertMessage; + }); + return message; +} + +@interface RCTViewComponentViewContainerTests : XCTestCase +@end + +@implementation RCTViewComponentViewContainerTests + +// When a view stops clipping, its children move from the container back into +// the view. The box shadow layer was already re-added by then, and +// `insertSubview:atIndex:` counts sublayers, so children appended after it were +// mounted one index too low from then on. +- (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers +{ + RCTViewComponentView *parent = [RCTViewComponentView new]; + applyProps(parent, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + + RCTViewComponentView *first = [RCTViewComponentView new]; + RCTViewComponentView *second = [RCTViewComponentView new]; + [parent mountChildComponentView:first index:0]; + [parent mountChildComponentView:second index:1]; + XCTAssertNotEqual(first.superview, parent, @"children of a clipping view with a box shadow go into a container"); + + applyProps(parent, makeProps(/* clips */ false, /* hasBoxShadow */ true)); + XCTAssertEqual(first.superview, parent); + XCTAssertEqual(parent.layer.sublayers.firstObject, first.layer, @"children must precede the box shadow layer"); + + RCTViewComponentView *third = [RCTViewComponentView new]; + [parent mountChildComponentView:third index:2]; + NSArray *expected = @[ first, second, third ]; + XCTAssertEqualObjects(parent.subviews, expected); + + XCTAssertNil(unmountAndReturnAssertMessage(parent, third, 2)); +} + +// A recycled view must not keep the container of its previous owner. Fabric +// mounts a view's children before it applies that view's props, so those +// children used to go into the stale container and were later moved above the +// next owner's box shadow layer. +- (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + RCTViewComponentView *previousChild = [RCTViewComponentView new]; + [view mountChildComponentView:previousChild index:0]; + UIView *previousContainer = previousChild.superview; + XCTAssertNotEqual(previousContainer, view); + [view unmountChildComponentView:previousChild index:0]; + + [view prepareForRecycle]; + XCTAssertNil(previousContainer.superview, @"the container is removed on recycle"); + XCTAssertEqual(view.subviews.count, 0u); + + // The next owner has a box shadow but doesn't clip. + RCTViewComponentView *first = [RCTViewComponentView new]; + [view mountChildComponentView:first index:0]; + XCTAssertEqual(first.superview, view); + applyProps(view, makeProps(/* clips */ false, /* hasBoxShadow */ true)); + XCTAssertFalse(view.clipsToBounds); + + RCTViewComponentView *second = [RCTViewComponentView new]; + [view mountChildComponentView:second index:1]; + NSArray *expected = @[ first, second ]; + XCTAssertEqualObjects(view.subviews, expected); + + XCTAssertNil(unmountAndReturnAssertMessage(view, second, 1)); +} + +// `updateProps` compares against the props kept from the previous owner, so a +// next owner that also clips gets no `overflow` update. The clipping that the +// container held must move back to the view when the container is dropped. +- (void)testRecycledViewKeepsClippingForNextOwnerThatClips +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + [view prepareForRecycle]; + + RCTViewComponentView *child = [RCTViewComponentView new]; + [view mountChildComponentView:child index:0]; + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ false)); + + XCTAssertEqual(child.superview, view); + XCTAssertTrue(view.clipsToBounds); +} + +// Subclasses such as image and text views put their own content view into the +// container. It has to come back with the children. +- (void)testRecycledViewMovesContentViewOutOfContainer +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + UIView *contentView = [UIView new]; + view.contentView = contentView; + XCTAssertNotEqual(contentView.superview, view); + + [view prepareForRecycle]; + + XCTAssertEqual(contentView.superview, view); +} + +@end diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm index 668a0404694d..df6dcb4236b0 100644 --- a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm @@ -86,10 +86,10 @@ - (void)testUnmountWithInBoundsMismatchStillReportsTagAtIndex XCTAssertNotEqualObjects(thrownName, NSRangeException); } -// A recycled view can receive children before its own props are updated. If -// the new owner is zero-sized, layer invalidation does not reconcile the old -// clipping container. Unmount must do so before checking the child's parent. -- (void)testUnmountReconcilesRecycledContainerAtZeroSize +// When a zero-sized view stops needing its clipping container, layer +// invalidation returns early and does not move the children out of it. Unmount +// must reconcile the container before checking the child's parent. +- (void)testUnmountReconcilesStaleContainerAtZeroSize { RCTViewComponentView *parent = [[RCTViewComponentView alloc] initWithFrame:CGRectZero]; auto clippingProps = std::make_shared(); @@ -98,17 +98,12 @@ - (void)testUnmountReconcilesRecycledContainerAtZeroSize [parent updateProps:clippingProps oldProps:parent.props]; [parent finalizeUpdates:RNComponentViewUpdateMaskAll]; - RCTViewComponentView *previousChild = [RCTViewComponentView new]; - [parent mountChildComponentView:previousChild index:0]; - UIView *previousContainer = previousChild.superview; + RCTViewComponentView *child = [RCTViewComponentView new]; + [parent mountChildComponentView:child index:0]; + UIView *previousContainer = child.superview; XCTAssertNotEqual(previousContainer, parent); XCTAssertNotNil(previousContainer); - [parent unmountChildComponentView:previousChild index:0]; - [parent prepareForRecycle]; - // Fabric mounts descendants before applying the recycled parent's new props. - RCTViewComponentView *child = [RCTViewComponentView new]; - [parent mountChildComponentView:child index:0]; [parent updateProps:std::make_shared() oldProps:parent.props]; [parent finalizeUpdates:RNComponentViewUpdateMaskAll]; XCTAssertTrue(CGSizeEqualToSize(parent.bounds.size, CGSizeZero)); From fdbd7355d8baa8454f649c1997aadd40fcdae3cb Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Thu, 26 Nov 2026 12:30:48 +0200 Subject: [PATCH 2/2] Match surrounding style in the container fix and its tests --- .../View/RCTViewComponentView.mm | 9 +++++---- .../RCTViewComponentViewContainerTests.mm | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) 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 e0abe9312742..9d25880346ee 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -893,7 +893,8 @@ - (void)prepareForRecycle // Drop the clipping container of the previous owner. Fabric mounts a view's // children before it applies that view's props, so a container left in place - // would receive the next owner's children. + // would receive the next owner's children. With the flag cleared, this getter + // moves any remaining subviews out and removes the container. _useCustomContainerView = NO; [self currentContainerView]; @@ -1159,9 +1160,9 @@ - (UIView *)currentContainerView // shadows, outlines), where they sit when no container is used. // `insertSubview:atIndex:` counts sublayers, so appending them after // those layers would shift every later mount by one. - NSUInteger index = 0; - for (UIView *subview in _containerView.subviews) { - [effectiveContentView insertSubview:subview atIndex:index++]; + NSArray *subviews = _containerView.subviews; + for (NSUInteger index = 0; index < subviews.count; index++) { + [effectiveContentView insertSubview:subviews[index] atIndex:index]; } effectiveContentView.clipsToBounds = _containerView.clipsToBounds; effectiveContentView.layer.mask = _containerView.layer.mask; diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm index 2e4839b05b5a..9f57146f1081 100644 --- a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm @@ -35,7 +35,9 @@ static void applyProps(RCTViewComponentView *view, const Props::Shared &props) { - [view updateProps:props oldProps:view.props]; + // Like the mounting layer does for an Insert, pass no old props: the view + // diffs against its own. + [view updateProps:props oldProps:nullptr]; LayoutMetrics layoutMetrics; layoutMetrics.frame.size = {100, 40}; [view updateLayoutMetrics:layoutMetrics oldLayoutMetrics:EmptyLayoutMetrics]; @@ -73,7 +75,7 @@ @implementation RCTViewComponentViewContainerTests - (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers { RCTViewComponentView *parent = [RCTViewComponentView new]; - applyProps(parent, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + applyProps(parent, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); RCTViewComponentView *first = [RCTViewComponentView new]; RCTViewComponentView *second = [RCTViewComponentView new]; @@ -81,7 +83,7 @@ - (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers [parent mountChildComponentView:second index:1]; XCTAssertNotEqual(first.superview, parent, @"children of a clipping view with a box shadow go into a container"); - applyProps(parent, makeProps(/* clips */ false, /* hasBoxShadow */ true)); + applyProps(parent, makeProps(/*clips=*/false, /*hasBoxShadow=*/true)); XCTAssertEqual(first.superview, parent); XCTAssertEqual(parent.layer.sublayers.firstObject, first.layer, @"children must precede the box shadow layer"); @@ -100,7 +102,7 @@ - (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers - (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren { RCTViewComponentView *view = [RCTViewComponentView new]; - applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); RCTViewComponentView *previousChild = [RCTViewComponentView new]; [view mountChildComponentView:previousChild index:0]; UIView *previousContainer = previousChild.superview; @@ -115,7 +117,7 @@ - (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren RCTViewComponentView *first = [RCTViewComponentView new]; [view mountChildComponentView:first index:0]; XCTAssertEqual(first.superview, view); - applyProps(view, makeProps(/* clips */ false, /* hasBoxShadow */ true)); + applyProps(view, makeProps(/*clips=*/false, /*hasBoxShadow=*/true)); XCTAssertFalse(view.clipsToBounds); RCTViewComponentView *second = [RCTViewComponentView new]; @@ -132,12 +134,12 @@ - (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren - (void)testRecycledViewKeepsClippingForNextOwnerThatClips { RCTViewComponentView *view = [RCTViewComponentView new]; - applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); [view prepareForRecycle]; RCTViewComponentView *child = [RCTViewComponentView new]; [view mountChildComponentView:child index:0]; - applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ false)); + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/false)); XCTAssertEqual(child.superview, view); XCTAssertTrue(view.clipsToBounds); @@ -148,7 +150,7 @@ - (void)testRecycledViewKeepsClippingForNextOwnerThatClips - (void)testRecycledViewMovesContentViewOutOfContainer { RCTViewComponentView *view = [RCTViewComponentView new]; - applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true)); + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); UIView *contentView = [UIView new]; view.contentView = contentView; XCTAssertNotEqual(contentView.superview, view);