Skip to content

Commit

Permalink
Match react-devtools display name behavior for React.memo
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 3, 2019
1 parent 4482fdd commit 48d8e0a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ describe('ReactDebugFiberPerf', () => {
const MemoFoo = React.memo(function Foo() {
return <div />;
});
MemoFoo.displayName = 'Foo';
ReactNoop.render(
<Parent>
<MemoFoo />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ describe('ReactLazy', () => {
Add.defaultProps = {
innerWithDefault: 42,
};
Add.displayName = 'Add';
await verifyInnerPropTypesAreChecked(Add);
});

Expand All @@ -787,6 +788,7 @@ describe('ReactLazy', () => {
Add.propTypes = {
inner: PropTypes.number.isRequired,
};
Add.displayName = 'Add';
await verifyInnerPropTypesAreChecked(Add);
});

Expand Down Expand Up @@ -847,7 +849,7 @@ describe('ReactLazy', () => {
expect(() => {
expect(Scheduler).toFlushAndYield(['Inner default text']);
}).toWarnDev(
'The prop `text` is marked as required in `T`, but its value is `undefined`',
'The prop `text` is marked as required in `Memo(T)`, but its value is `undefined`',
);
expect(root).toMatchRenderedOutput('Inner default text');

Expand All @@ -860,7 +862,7 @@ describe('ReactLazy', () => {
);
expect(Scheduler).toFlushAndYield([null]);
}).toWarnDev(
'The prop `text` is marked as required in `T`, but its value is `null`',
'The prop `text` is marked as required in `Memo(T)`, but its value is `null`',
);
expect(root).toMatchRenderedOutput(null);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ describe('memo', () => {
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev(
// Outer props are checked in createElement
'Invalid prop `outer` of type `string` supplied to `FnInner`, expected `number`.',
'Invalid prop `outer` of type `string` supplied to `Memo(FnInner)`, expected `number`.',
);

// Update
Expand All @@ -372,7 +372,7 @@ describe('memo', () => {
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev(
// Outer props are checked in createElement
'Invalid prop `outer` of type `boolean` supplied to `FnInner`, expected `number`.',
'Invalid prop `outer` of type `boolean` supplied to `Memo(FnInner)`, expected `number`.',
);
});

Expand All @@ -398,8 +398,8 @@ describe('memo', () => {
ReactNoop.render(<Outer inner="2" middle="3" outer="4" />);
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev([
'Invalid prop `outer` of type `string` supplied to `Inner`, expected `number`.',
'Invalid prop `middle` of type `string` supplied to `Inner`, expected `number`.',
'Invalid prop `outer` of type `string` supplied to `Memo(Memo(Inner))`, expected `number`.',
'Invalid prop `middle` of type `string` supplied to `Memo(Inner)`, expected `number`.',
'Invalid prop `inner` of type `string` supplied to `Inner`, expected `number`.',
]);

Expand All @@ -410,8 +410,8 @@ describe('memo', () => {
);
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev([
'Invalid prop `outer` of type `boolean` supplied to `Inner`, expected `number`.',
'Invalid prop `middle` of type `boolean` supplied to `Inner`, expected `number`.',
'Invalid prop `outer` of type `boolean` supplied to `Memo(Memo(Inner))`, expected `number`.',
'Invalid prop `middle` of type `boolean` supplied to `Memo(Inner)`, expected `number`.',
'Invalid prop `inner` of type `boolean` supplied to `Inner`, expected `number`.',
]);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/getComponentName.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ function getWrappedName(
innerType: any,
wrapperName: string,
): string {
const functionName = innerType.displayName || innerType.name || '';
const innerName = getComponentName(innerType) || '';
return (
(outerType: any).displayName ||
(functionName !== '' ? `${wrapperName}(${functionName})` : wrapperName)
(innerName !== '' ? `${wrapperName}(${innerName})` : wrapperName)
);
}

Expand Down Expand Up @@ -80,7 +80,7 @@ function getComponentName(type: mixed): string | null {
case REACT_FORWARD_REF_TYPE:
return getWrappedName(type, type.render, 'ForwardRef');
case REACT_MEMO_TYPE:
return getComponentName(type.type);
return getWrappedName(type, type.type, 'Memo');
case REACT_LAZY_TYPE: {
const thenable: LazyComponent<mixed> = (type: any);
const resolvedThenable = refineResolvedLazyComponent(thenable);
Expand Down

0 comments on commit 48d8e0a

Please sign in to comment.