Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/react/src/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ function validatePropTypes(element) {
) {
// ForwardRef
const functionName = type.render.displayName || type.render.name || '';
name = functionName !== '' ? `ForwardRef(${functionName})` : 'ForwardRef';
name =
type.displayName ||
(functionName !== '' ? `ForwardRef(${functionName})` : 'ForwardRef');
propTypes = type.propTypes;
} else {
return;
Expand Down
29 changes: 29 additions & 0 deletions packages/react/src/__tests__/forwardRef-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,33 @@ describe('forwardRef', () => {
{withoutStack: true},
);
});

it('should honor a displayName if set on the forwardRef wrapper in warnings', () => {
const Component = props => <div {...props} />;

const RefForwardingComponent = React.forwardRef((props, ref) => (
<Component {...props} forwardedRef={ref} />
));

RefForwardingComponent.displayName = 'Foo';

RefForwardingComponent.propTypes = {
optional: PropTypes.string,
required: PropTypes.string.isRequired,
};

RefForwardingComponent.defaultProps = {
optional: 'default',
};

const ref = React.createRef();

expect(() =>
ReactNoop.render(<RefForwardingComponent ref={ref} optional="foo" />),
).toWarnDev(
'Warning: Failed prop type: The prop `required` is marked as required in ' +
'`Foo`, but its value is `undefined`.\n' +
' in Foo (at **)',
);
});
});
7 changes: 4 additions & 3 deletions packages/shared/getComponentName.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ function getComponentName(type: mixed): string | null {
case REACT_FORWARD_REF_TYPE:
const renderFn = (type.render: any);
const functionName = renderFn.displayName || renderFn.name || '';
return functionName !== ''
? `ForwardRef(${functionName})`
: 'ForwardRef';
return (
(type: any).displayName ||
(functionName !== '' ? `ForwardRef(${functionName})` : 'ForwardRef')
);
}
if (typeof type.then === 'function') {
const thenable: Thenable<mixed> = (type: any);
Expand Down