diff --git a/React/Views/RCTShadowView.h b/React/Views/RCTShadowView.h index bcf0522f82a..90f21c86355 100644 --- a/React/Views/RCTShadowView.h +++ b/React/Views/RCTShadowView.h @@ -217,9 +217,22 @@ typedef void (^RCTApplierBlock)(NSDictionary *viewRegistry absolutePosition:(CGPoint)absolutePosition; /** - * Return whether or not this node acts as a leaf node in the eyes of Yoga. + * Returns whether or not this view can have any subviews. + * Adding/inserting a child view to leaf view (`canHaveSubviews` equals `NO`) + * will throw an error. + * Return `NO` for components which must not have any descendants + * (like , for example.) + * Defaults to `YES`. Can be overridden in subclasses. + * Don't confuse this with `isYogaLeafNode`. + */ +- (BOOL)canHaveSubviews; + +/** + * Returns whether or not this node acts as a leaf node in the eyes of Yoga. * For example `RCTShadowText` has children which it does not want Yoga * to lay out so in the eyes of Yoga it is a leaf node. + * Defaults to `NO`. Can be overridden in subclasses. + * Don't confuse this with `canHaveSubviews`. */ - (BOOL)isYogaLeafNode; diff --git a/React/Views/RCTShadowView.m b/React/Views/RCTShadowView.m index 61b992cb1ca..2e7d4794c65 100644 --- a/React/Views/RCTShadowView.m +++ b/React/Views/RCTShadowView.m @@ -365,6 +365,11 @@ static void RCTProcessMetaPropsBorder(const YGValue metaProps[META_PROP_COUNT], YGNodeFree(_yogaNode); } +- (BOOL)canHaveSubviews +{ + return NO; +} + - (BOOL)isYogaLeafNode { return NO; @@ -403,6 +408,8 @@ static void RCTProcessMetaPropsBorder(const YGValue metaProps[META_PROP_COUNT], - (void)insertReactSubview:(RCTShadowView *)subview atIndex:(NSInteger)atIndex { + RCTAssert(!self.canHaveSubviews, @"Attempt to insert subview inside leaf view."); + [_reactSubviews insertObject:subview atIndex:atIndex]; if (![self isYogaLeafNode]) { YGNodeInsertChild(_yogaNode, subview.yogaNode, (uint32_t)atIndex);