Skip to content

Commit debaf42

Browse files
authored
Fix iOS Fabric unmount assertions for zero-sized recycled views
## Summary: Related to #59166. This change addresses the unmount comparison; it does not reset container state during recycling. Unmounting a zero-sized recycled `View` can fail with `Attempt to unmount a view which is mounted inside a different view`, even when the exception reports the parent's own tag as the existing parent. A previous owner can leave the native view with a clipping container, for example when using `overflow: 'hidden'` with an outline. Fabric can mount descendants before applying the recycled parent's new props. If those props no longer require the container and the view has zero bounds, layer invalidation returns without migrating the children back. `currentContainerView` performs that migration lazily. The unmount assertion compares `childComponentView.superview == self.currentContainerView`, so evaluation can read the child's old superview before the getter moves it. By the time the exception message is formatted, the child can already belong to the expected parent. Resolve the container in a separate statement before reading `superview` in the assertions. The reconciliation also runs when assertions are disabled. Existing ownership and index checks remain unchanged. <img width="2771" height="1728" alt="01" src="https://github.com/user-attachments/assets/e7a6e3ed-9132-405c-bb5c-da2469dad1c6" /> ## Changelog: [IOS] [FIXED] - Reconcile recycled view containers before checking child ownership during Fabric unmount. ## Test Plan: Added native regression tests for: - A clipping-container owner being recycled, receiving a new child before its props update, and then being unmounted at zero size after the container is no longer required. The test checks that unmount does not assert, detaches the child, and removes the stale container. - A child that actually belongs to another parent still producing an ownership assertion. The lifecycle sequence above is constructed directly with `RCTViewComponentView`; it is not an end-to-end JavaScript/Fabric reproduction. Its assertion behavior can depend on operand evaluation order before the fix. The `RCTViewComponentViewUnmountTests.mm` passed. Real application tested on `react-native` 0.88.1 with iOS 27.2 device, Xcode 27.1. Differential Revision: D128635669 Pull Request resolved: #59628
1 parent 7230b63 commit debaf42

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,24 @@ - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childCompo
243243
if (_removeClippedSubviews) {
244244
[_reactSubviews removeObjectAtIndex:index];
245245
} else {
246+
// This getter can migrate children from a recycled clipping container.
247+
// Reconcile before reading superview, even when assertions are disabled.
248+
[[maybe_unused]] UIView *containerView = self.currentContainerView;
246249
RCTAssert(
247250
childComponentView.superview != nil,
248251
@"Attempt to unmount a view which is not mounted. (parent: %@, child: %@, index: %@)",
249252
self,
250253
childComponentView,
251254
@(index));
252255
RCTAssert(
253-
childComponentView.superview == self.currentContainerView,
256+
childComponentView.superview == containerView,
254257
@"Attempt to unmount a view which is mounted inside a different view. (parent: %@, child: %@, index: %@, existing parent: %@)",
255258
self,
256259
childComponentView,
257260
@(index),
258261
@([childComponentView.superview tag]));
259262
#ifndef NS_BLOCK_ASSERTIONS
260-
NSArray<UIView *> *containerSubviews = self.currentContainerView.subviews;
263+
NSArray<UIView *> *containerSubviews = containerView.subviews;
261264
BOOL isIndexInBounds = index >= 0 && (NSUInteger)index < containerSubviews.count;
262265
RCTAssert(
263266
isIndexInBounds && [containerSubviews objectAtIndex:index] == childComponentView,

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#import <React/RCTAssert.h>
99
#import <React/RCTViewComponentView.h>
1010
#import <XCTest/XCTest.h>
11+
#import <react/renderer/components/view/ViewProps.h>
12+
13+
using namespace facebook::react;
1114

1215
@interface RCTViewComponentViewUnmountTests : XCTestCase
1316
@end
@@ -83,4 +86,54 @@ - (void)testUnmountWithInBoundsMismatchStillReportsTagAtIndex
8386
XCTAssertNotEqualObjects(thrownName, NSRangeException);
8487
}
8588

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
93+
{
94+
RCTViewComponentView *parent = [[RCTViewComponentView alloc] initWithFrame:CGRectZero];
95+
auto clippingProps = std::make_shared<ViewProps>();
96+
clippingProps->yogaStyle.setOverflow(facebook::yoga::Overflow::Hidden);
97+
clippingProps->outlineWidth = 1;
98+
[parent updateProps:clippingProps oldProps:parent.props];
99+
[parent finalizeUpdates:RNComponentViewUpdateMaskAll];
100+
101+
RCTViewComponentView *previousChild = [RCTViewComponentView new];
102+
[parent mountChildComponentView:previousChild index:0];
103+
UIView *previousContainer = previousChild.superview;
104+
XCTAssertNotEqual(previousContainer, parent);
105+
XCTAssertNotNil(previousContainer);
106+
[parent unmountChildComponentView:previousChild index:0];
107+
[parent prepareForRecycle];
108+
109+
// Fabric mounts descendants before applying the recycled parent's new props.
110+
RCTViewComponentView *child = [RCTViewComponentView new];
111+
[parent mountChildComponentView:child index:0];
112+
[parent updateProps:std::make_shared<ViewProps>() oldProps:parent.props];
113+
[parent finalizeUpdates:RNComponentViewUpdateMaskAll];
114+
XCTAssertTrue(CGSizeEqualToSize(parent.bounds.size, CGSizeZero));
115+
XCTAssertEqual(child.superview, previousContainer);
116+
117+
NSString *message = nil;
118+
NSString *thrownName = RCTUnmountAndReportAssert(parent, child, 0, &message);
119+
120+
XCTAssertNil(message, @"Container migration must precede the ownership check");
121+
XCTAssertNil(thrownName);
122+
XCTAssertNil(child.superview);
123+
XCTAssertNil(previousContainer.superview, @"The stale container must be removed");
124+
}
125+
126+
- (void)testUnmountStillReportsChildMountedUnderAnotherParent
127+
{
128+
RCTViewComponentView *parent = [RCTViewComponentView new];
129+
RCTViewComponentView *otherParent = [RCTViewComponentView new];
130+
RCTViewComponentView *child = [RCTViewComponentView new];
131+
[otherParent mountChildComponentView:child index:0];
132+
133+
NSString *message = nil;
134+
RCTUnmountAndReportAssert(parent, child, 0, &message);
135+
136+
XCTAssertTrue([message containsString:@"mounted inside a different view"]);
137+
}
138+
86139
@end

0 commit comments

Comments
 (0)