Skip to content

Commit ddf2c2f

Browse files
sahrensfacebook-github-bot
authored andcommitted
fix forwardRef displayName on Text and View
Reviewed By: TheSavior Differential Revision: D8342852 fbshipit-source-id: 5af80edfd5de5b6d6ea6fdc24abf8931f767c812
1 parent d0219a0 commit ddf2c2f

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

Libraries/Components/View/View.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,24 @@ const RCTView = requireNativeComponent('RCTView');
3232

3333
let ViewToExport = RCTView;
3434
if (__DEV__) {
35+
const View = (props: Props, forwardedRef: ?React.Ref<'RCTView'>) => {
36+
return (
37+
<TextAncestor.Consumer>
38+
{hasTextAncestor => {
39+
invariant(
40+
!hasTextAncestor,
41+
'Nesting of <View> within <Text> is not currently supported.',
42+
);
43+
return <RCTView {...props} ref={forwardedRef} />;
44+
}}
45+
</TextAncestor.Consumer>
46+
);
47+
};
48+
View.displayName = 'View'; // TODO(T30332650) remove bug workaround
3549
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
36-
ViewToExport = React.forwardRef((props, ref) => (
37-
<TextAncestor.Consumer>
38-
{hasTextAncestor => {
39-
invariant(
40-
!hasTextAncestor,
41-
'Nesting of <View> within <Text> is not currently supported.',
42-
);
43-
return <RCTView {...props} ref={ref} />;
44-
}}
45-
</TextAncestor.Consumer>
46-
));
47-
ViewToExport.displayName = 'View';
50+
ViewToExport = React.forwardRef(View);
4851
}
4952

50-
module.exports = ((ViewToExport: any): Class<NativeComponent<ViewProps>>);
53+
module.exports = ((ViewToExport: $FlowFixMe): Class<
54+
NativeComponent<ViewProps>,
55+
>);

Libraries/Text/Text.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,17 @@ const RCTVirtualText =
261261
uiViewClassName: 'RCTVirtualText',
262262
}));
263263

264+
const Text = (
265+
props: TextProps,
266+
forwardedRef: ?React.Ref<'RCTText' | 'RCTVirtualText'>,
267+
) => {
268+
return <TouchableText {...props} forwardedRef={forwardedRef} />;
269+
};
270+
Text.displayName = 'Text'; // TODO(T30332650) remove bug workaround
264271
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
265-
const Text = React.forwardRef((props, ref) => (
266-
<TouchableText {...props} forwardedRef={ref} />
267-
));
268-
Text.displayName = 'Text';
272+
const TextToExport = React.forwardRef(Text);
269273

270274
// TODO: Deprecate this.
271-
Text.propTypes = TextPropTypes;
275+
TextToExport.propTypes = TextPropTypes;
272276

273-
module.exports = ((Text: any): Class<NativeComponent<TextProps>>);
277+
module.exports = (TextToExport: Class<NativeComponent<TextProps>>);

jest/mockComponent.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ module.exports = (moduleName, instanceMethods) => {
1818

1919
const Component = class extends SuperClass {
2020
render() {
21-
const name = RealComponent.displayName || RealComponent.name;
21+
const name =
22+
RealComponent.displayName ||
23+
RealComponent.name ||
24+
(RealComponent.render // handle React.forwardRef
25+
? RealComponent.render.displayName || RealComponent.render.name
26+
: 'Unknown');
2227

2328
const props = Object.assign({}, RealComponent.defaultProps);
2429

0 commit comments

Comments
 (0)