Skip to content

Commit 36c1064

Browse files
committed
[rn] Pass props when creating a view in RCTViewManager
1 parent 27a67c2 commit 36c1064

5 files changed

Lines changed: 19 additions & 5 deletions

File tree

React/Modules/RCTUIManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ - (void)_manageChildren:(NSNumber *)containerReactTag
758758
UIColor *backgroundColor = shadowView.backgroundColor;
759759

760760
[self addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry){
761-
id<RCTComponent> view = [componentData createViewWithTag:reactTag];
761+
id<RCTComponent> view = [componentData createViewWithTag:reactTag props:props];
762762
if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
763763
((UIView *)view).backgroundColor = backgroundColor;
764764
}

React/Views/RCTComponentData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
- (instancetype)initWithManager:(RCTViewManager *)manager NS_DESIGNATED_INITIALIZER;
2424

25-
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag;
25+
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag props:(NSDictionary *)props;
2626
- (RCTShadowView *)createShadowViewWithTag:(NSNumber *)tag;
2727
- (void)setProps:(NSDictionary *)props forView:(id<RCTComponent>)view;
2828
- (void)setProps:(NSDictionary *)props forShadowView:(RCTShadowView *)shadowView;

React/Views/RCTComponentData.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ - (instancetype)initWithManager:(RCTViewManager *)manager
6262

6363
RCT_NOT_IMPLEMENTED(- (instancetype)init)
6464

65-
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag
65+
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag props:(NSDictionary *)props
6666
{
6767
RCTAssertMainThread();
6868

69-
id<RCTComponent> view = (id<RCTComponent>)[_manager view];
69+
id<RCTComponent> view = (id<RCTComponent>)(props ? [_manager viewWithProps:props] : [_manager view]);
7070
view.reactTag = tag;
7171
if ([view isKindOfClass:[UIView class]]) {
7272
((UIView *)view).multipleTouchEnabled = YES;
@@ -264,7 +264,7 @@ - (void)setProps:(NSDictionary *)props forView:(id<RCTComponent>)view
264264
}
265265

266266
if (!_defaultView) {
267-
_defaultView = [self createViewWithTag:nil];
267+
_defaultView = [self createViewWithTag:nil props:nil];
268268
}
269269

270270
[props enumerateKeysAndObjectsUsingBlock:^(NSString *key, id json, __unused BOOL *stop) {

React/Views/RCTViewManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ typedef void (^RCTViewManagerUIBlock)(RCTUIManager *uiManager, RCTSparseArray *v
3939
*/
4040
- (UIView *)view;
4141

42+
/**
43+
* This method instantiates a native view using the props passed into the component.
44+
* This method should be used when you need to know about specific props in order to
45+
* initialize a view. By default, this just calls the -view method. Each prop will
46+
* still be set individually, after the view is created. Like the -view method,
47+
* -viewWithProps: should return a fresh instance each time it is called.
48+
*/
49+
- (UIView *)viewWithProps:(NSDictionary *)props;
50+
4251
/**
4352
* This method instantiates a shadow view to be managed by the module. If omitted,
4453
* an ordinary RCTShadowView instance will be created, which is typically fine for

React/Views/RCTViewManager.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ - (dispatch_queue_t)methodQueue
5454
return _bridge.uiManager.methodQueue;
5555
}
5656

57+
- (UIView *)viewWithProps:(NSDictionary *)props
58+
{
59+
return [self view];
60+
}
61+
5762
- (UIView *)view
5863
{
5964
return [RCTView new];

0 commit comments

Comments
 (0)