Skip to content

Commit 63243ca

Browse files
committed
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.
1 parent 63835d3 commit 63243ca

3 files changed

Lines changed: 180 additions & 13 deletions

File tree

‎packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,12 @@ - (void)prepareForRecycle
891891
_filterLayer = nil;
892892
[self clearExistingBackgroundImageLayers];
893893
894+
// Drop the clipping container of the previous owner. Fabric mounts a view's
895+
// children before it applies that view's props, so a container left in place
896+
// would receive the next owner's children.
897+
_useCustomContainerView = NO;
898+
[self currentContainerView];
899+
894900
[self _setLastSentSafeAreaInsets:nil];
895901
_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil;
896902
_eventEmitter.reset();
@@ -1149,8 +1155,13 @@ - (UIView *)currentContainerView
11491155
effectiveContentView = _containerView;
11501156
} else {
11511157
if (_containerView) {
1158+
// Put the children back below the layers this view draws itself (box
1159+
// shadows, outlines), where they sit when no container is used.
1160+
// `insertSubview:atIndex:` counts sublayers, so appending them after
1161+
// those layers would shift every later mount by one.
1162+
NSUInteger index = 0;
11521163
for (UIView *subview in _containerView.subviews) {
1153-
[effectiveContentView addSubview:subview];
1164+
[effectiveContentView insertSubview:subview atIndex:index++];
11541165
}
11551166
effectiveContentView.clipsToBounds = _containerView.clipsToBounds;
11561167
effectiveContentView.layer.mask = _containerView.layer.mask;
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <React/RCTAssert.h>
9+
#import <React/RCTViewComponentView.h>
10+
#import <XCTest/XCTest.h>
11+
#import <react/renderer/components/view/ViewProps.h>
12+
#import <react/renderer/core/LayoutMetrics.h>
13+
#import <react/renderer/graphics/Color.h>
14+
15+
using namespace facebook::react;
16+
17+
// A view that clips and has a box shadow draws the shadow outside its bounds,
18+
// so RCTViewComponentView moves its children into a separate clipping
19+
// container view.
20+
static Props::Shared makeProps(bool clips, bool hasBoxShadow)
21+
{
22+
auto props = std::make_shared<ViewProps>();
23+
if (clips) {
24+
props->yogaStyle.setOverflow(facebook::yoga::Overflow::Hidden);
25+
}
26+
if (hasBoxShadow) {
27+
BoxShadow shadow;
28+
shadow.offsetY = 2;
29+
shadow.blurRadius = 6;
30+
shadow.color = colorFromRGBA(0, 0, 0, 102);
31+
props->boxShadow = {shadow};
32+
}
33+
return props;
34+
}
35+
36+
static void applyProps(RCTViewComponentView *view, const Props::Shared &props)
37+
{
38+
[view updateProps:props oldProps:view.props];
39+
LayoutMetrics layoutMetrics;
40+
layoutMetrics.frame.size = {100, 40};
41+
[view updateLayoutMetrics:layoutMetrics oldLayoutMetrics:EmptyLayoutMetrics];
42+
[view finalizeUpdates:RNComponentViewUpdateMaskAll];
43+
}
44+
45+
// Unmounts `child` and returns the message of any RCTAssert it raised, or nil.
46+
static NSString *
47+
unmountAndReturnAssertMessage(RCTViewComponentView *parent, UIView<RCTComponentViewProtocol> *child, NSInteger index)
48+
{
49+
__block NSString *message = nil;
50+
RCTPerformBlockWithAssertFunction(
51+
^{
52+
@try {
53+
[parent unmountChildComponentView:child index:index];
54+
} @catch (NSException *exception) {
55+
message = message ?: exception.reason;
56+
}
57+
},
58+
^(NSString *condition, NSString *fileName, NSNumber *lineNumber, NSString *function, NSString *assertMessage) {
59+
message = assertMessage;
60+
});
61+
return message;
62+
}
63+
64+
@interface RCTViewComponentViewContainerTests : XCTestCase
65+
@end
66+
67+
@implementation RCTViewComponentViewContainerTests
68+
69+
// When a view stops clipping, its children move from the container back into
70+
// the view. The box shadow layer was already re-added by then, and
71+
// `insertSubview:atIndex:` counts sublayers, so children appended after it were
72+
// mounted one index too low from then on.
73+
- (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers
74+
{
75+
RCTViewComponentView *parent = [RCTViewComponentView new];
76+
applyProps(parent, makeProps(/* clips */ true, /* hasBoxShadow */ true));
77+
78+
RCTViewComponentView *first = [RCTViewComponentView new];
79+
RCTViewComponentView *second = [RCTViewComponentView new];
80+
[parent mountChildComponentView:first index:0];
81+
[parent mountChildComponentView:second index:1];
82+
XCTAssertNotEqual(first.superview, parent, @"children of a clipping view with a box shadow go into a container");
83+
84+
applyProps(parent, makeProps(/* clips */ false, /* hasBoxShadow */ true));
85+
XCTAssertEqual(first.superview, parent);
86+
XCTAssertEqual(parent.layer.sublayers.firstObject, first.layer, @"children must precede the box shadow layer");
87+
88+
RCTViewComponentView *third = [RCTViewComponentView new];
89+
[parent mountChildComponentView:third index:2];
90+
NSArray *expected = @[ first, second, third ];
91+
XCTAssertEqualObjects(parent.subviews, expected);
92+
93+
XCTAssertNil(unmountAndReturnAssertMessage(parent, third, 2));
94+
}
95+
96+
// A recycled view must not keep the container of its previous owner. Fabric
97+
// mounts a view's children before it applies that view's props, so those
98+
// children used to go into the stale container and were later moved above the
99+
// next owner's box shadow layer.
100+
- (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren
101+
{
102+
RCTViewComponentView *view = [RCTViewComponentView new];
103+
applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true));
104+
RCTViewComponentView *previousChild = [RCTViewComponentView new];
105+
[view mountChildComponentView:previousChild index:0];
106+
UIView *previousContainer = previousChild.superview;
107+
XCTAssertNotEqual(previousContainer, view);
108+
[view unmountChildComponentView:previousChild index:0];
109+
110+
[view prepareForRecycle];
111+
XCTAssertNil(previousContainer.superview, @"the container is removed on recycle");
112+
XCTAssertEqual(view.subviews.count, 0u);
113+
114+
// The next owner has a box shadow but doesn't clip.
115+
RCTViewComponentView *first = [RCTViewComponentView new];
116+
[view mountChildComponentView:first index:0];
117+
XCTAssertEqual(first.superview, view);
118+
applyProps(view, makeProps(/* clips */ false, /* hasBoxShadow */ true));
119+
XCTAssertFalse(view.clipsToBounds);
120+
121+
RCTViewComponentView *second = [RCTViewComponentView new];
122+
[view mountChildComponentView:second index:1];
123+
NSArray *expected = @[ first, second ];
124+
XCTAssertEqualObjects(view.subviews, expected);
125+
126+
XCTAssertNil(unmountAndReturnAssertMessage(view, second, 1));
127+
}
128+
129+
// `updateProps` compares against the props kept from the previous owner, so a
130+
// next owner that also clips gets no `overflow` update. The clipping that the
131+
// container held must move back to the view when the container is dropped.
132+
- (void)testRecycledViewKeepsClippingForNextOwnerThatClips
133+
{
134+
RCTViewComponentView *view = [RCTViewComponentView new];
135+
applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true));
136+
[view prepareForRecycle];
137+
138+
RCTViewComponentView *child = [RCTViewComponentView new];
139+
[view mountChildComponentView:child index:0];
140+
applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ false));
141+
142+
XCTAssertEqual(child.superview, view);
143+
XCTAssertTrue(view.clipsToBounds);
144+
}
145+
146+
// Subclasses such as image and text views put their own content view into the
147+
// container. It has to come back with the children.
148+
- (void)testRecycledViewMovesContentViewOutOfContainer
149+
{
150+
RCTViewComponentView *view = [RCTViewComponentView new];
151+
applyProps(view, makeProps(/* clips */ true, /* hasBoxShadow */ true));
152+
UIView *contentView = [UIView new];
153+
view.contentView = contentView;
154+
XCTAssertNotEqual(contentView.superview, view);
155+
156+
[view prepareForRecycle];
157+
158+
XCTAssertEqual(contentView.superview, view);
159+
}
160+
161+
@end

‎packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ - (void)testUnmountWithInBoundsMismatchStillReportsTagAtIndex
8686
XCTAssertNotEqualObjects(thrownName, NSRangeException);
8787
}
8888
89-
// A recycled view can receive children before its own props are updated. If
90-
// the new owner is zero-sized, layer invalidation does not reconcile the old
91-
// clipping container. Unmount must do so before checking the child's parent.
92-
- (void)testUnmountReconcilesRecycledContainerAtZeroSize
89+
// When a zero-sized view stops needing its clipping container, layer
90+
// invalidation returns early and does not move the children out of it. Unmount
91+
// must reconcile the container before checking the child's parent.
92+
- (void)testUnmountReconcilesStaleContainerAtZeroSize
9393
{
9494
RCTViewComponentView *parent = [[RCTViewComponentView alloc] initWithFrame:CGRectZero];
9595
auto clippingProps = std::make_shared<ViewProps>();
@@ -98,17 +98,12 @@ - (void)testUnmountReconcilesRecycledContainerAtZeroSize
9898
[parent updateProps:clippingProps oldProps:parent.props];
9999
[parent finalizeUpdates:RNComponentViewUpdateMaskAll];
100100
101-
RCTViewComponentView *previousChild = [RCTViewComponentView new];
102-
[parent mountChildComponentView:previousChild index:0];
103-
UIView *previousContainer = previousChild.superview;
101+
RCTViewComponentView *child = [RCTViewComponentView new];
102+
[parent mountChildComponentView:child index:0];
103+
UIView *previousContainer = child.superview;
104104
XCTAssertNotEqual(previousContainer, parent);
105105
XCTAssertNotNil(previousContainer);
106-
[parent unmountChildComponentView:previousChild index:0];
107-
[parent prepareForRecycle];
108106
109-
// Fabric mounts descendants before applying the recycled parent's new props.
110-
RCTViewComponentView *child = [RCTViewComponentView new];
111-
[parent mountChildComponentView:child index:0];
112107
[parent updateProps:std::make_shared<ViewProps>() oldProps:parent.props];
113108
[parent finalizeUpdates:RNComponentViewUpdateMaskAll];
114109
XCTAssertTrue(CGSizeEqualToSize(parent.bounds.size, CGSizeZero));

0 commit comments

Comments
 (0)