Skip to content

Commit

Permalink
Fix LazilyLoadView lookup so that it can drop RCT prefixes.
Browse files Browse the repository at this point in the history
Summary:
While debugging internally, we have found that modules are almost always registered
with their "RK" or "RCT" prefixes dropped.

However, if a view is named `RCTFooView` and needs `RCTFooViewManager` to render natively, it will almost never find it because `RCT` was dropped from the key to the ViewManager instance.

In the event you look for a `ViewManager` and don't find it, this strips any "React" prefixes from your key and tries ones more time.

Reviewed By: spredolac

Differential Revision: D10734005

fbshipit-source-id: 2bfa6f19830f14f09af2fe7dc7e44b7e26e0ac3f
  • Loading branch information
d16r authored and facebook-github-bot committed Oct 26, 2018
1 parent 1b4fd64 commit 6534718
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
8 changes: 1 addition & 7 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,7 @@ void RCTRegisterModule(Class moduleClass)
name = NSStringFromClass(cls);
}

if ([name hasPrefix:@"RK"]) {
name = [name substringFromIndex:2];
} else if ([name hasPrefix:@"RCT"]) {
name = [name substringFromIndex:3];
}

return name;
return RCTDropReactPrefixes(name);
}

static BOOL jsiNativeModuleEnabled = NO;
Expand Down
3 changes: 3 additions & 0 deletions React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,7 @@ RCT_EXTERN NSString *RCTUIKitLocalizedString(NSString *string);
RCT_EXTERN NSString *__nullable RCTGetURLQueryParam(NSURL *__nullable URL, NSString *param);
RCT_EXTERN NSURL *__nullable RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *param, NSString *__nullable value);

// Given a string, drop common RN prefixes (RCT, RK, etc.)
RCT_EXTERN NSString *RCTDropReactPrefixes(NSString *s);

NS_ASSUME_NONNULL_END
11 changes: 11 additions & 0 deletions React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,14 @@ static void RCTGetRGBAColorComponents(CGColorRef color, CGFloat rgba[4])
components.queryItems = queryItems;
return components.URL;
}

RCT_EXTERN NSString *RCTDropReactPrefixes(NSString *s)
{
if ([s hasPrefix:@"RK"]) {
return [s substringFromIndex:2];
} else if ([s hasPrefix:@"RCT"]) {
return [s substringFromIndex:3];
}

return s;
}
8 changes: 8 additions & 0 deletions React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,14 @@ static void RCTMeasureLayout(RCTShadowView *view,
}

id module = [self.bridge moduleForName:moduleName];
if (module == nil) {
// There is all sorts of code in this codebase that drops prefixes.
//
// If we didn't find a module, it's possible because it's stored under a key
// which had RCT Prefixes stripped. Lets check one more time...
module = [self.bridge moduleForName:RCTDropReactPrefixes(moduleName)];
}

RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:[module class] bridge:self.bridge];
_componentDataByName[componentData.name] = componentData;
NSMutableDictionary *directEvents = [NSMutableDictionary new];
Expand Down

0 comments on commit 6534718

Please sign in to comment.