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 777428c3772e..c9c2520451aa 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -51,6 +51,7 @@ @implementation RCTViewComponentView { UIView *_containerView; BOOL _useCustomContainerView; NSMutableSet *_accessibilityOrderNativeIDs; + NSMutableArray *_accessibilityElements; RCTViewAccessibilityElement *_axElementDescribingSelf; } @@ -403,6 +404,8 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared & for (const std::string &childId : newViewProps.accessibilityOrder) { [_accessibilityOrderNativeIDs addObject:RCTNSStringFromString(childId)]; } + + _accessibilityElements = [NSMutableArray new]; } // `accessibilityTraits` @@ -614,6 +617,7 @@ - (void)prepareForRecycle _isJSResponder = NO; _removeClippedSubviews = NO; _reactSubviews = [NSMutableArray new]; + _accessibilityElements = [NSMutableArray new]; } - (void)setPropKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN:(NSSet *_Nullable)props @@ -1151,13 +1155,19 @@ - (NSObject *)accessibilityElement return super.accessibilityElements; } + // TODO: Currently this ignores changes to descendant nativeID's. While that should rarely, if ever happen, it's an + // edge case we should address. Currently this fixes some app deaths so landing this without addressing that edge case + // for now. + if ([_accessibilityElements count] > 0) { + return _accessibilityElements; + } + NSMutableDictionary *nativeIdToView = [NSMutableDictionary new]; [RCTViewComponentView collectAccessibilityElements:self intoDictionary:nativeIdToView nativeIds:_accessibilityOrderNativeIDs]; - NSMutableArray *elements = [NSMutableArray new]; for (auto childId : _props->accessibilityOrder) { NSString *nsStringChildId = RCTNSStringFromString(childId); // Special case to allow for self-referencing with accessibilityOrder @@ -1166,16 +1176,16 @@ - (NSObject *)accessibilityElement _axElementDescribingSelf = [[RCTViewAccessibilityElement alloc] initWithView:self]; } _axElementDescribingSelf.isAccessibilityElement = [super isAccessibilityElement]; - [elements addObject:_axElementDescribingSelf]; + [_accessibilityElements addObject:_axElementDescribingSelf]; } else { UIView *viewWithMatchingNativeId = [nativeIdToView objectForKey:nsStringChildId]; if (viewWithMatchingNativeId) { - [elements addObject:viewWithMatchingNativeId]; + [_accessibilityElements addObject:viewWithMatchingNativeId]; } } } - return elements; + return _accessibilityElements; } + (void)collectAccessibilityElements:(UIView *)view