Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Update getDisplayName to handle React.memo #19762

Merged
merged 3 commits into from
Feb 19, 2020
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/material-ui-utils/src/getDisplayName.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ForwardRef } from 'react-is';
import { ForwardRef, Memo } from 'react-is';

// Simplified polyfill for IE 11 support
// https://github.com/JamesMGreene/Function.name/blob/58b314d4a983110c3682f1228f845d39ccca1817/Function.name.js#L3
Expand Down Expand Up @@ -50,6 +50,8 @@ export default function getDisplayName(Component) {
switch (Component.$$typeof) {
Copy link
Member

@oliviertassinari oliviertassinari Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, could isFragment and isMemo be used instead to avoid React's internals access?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried switching to typeOf(Component), however IIRC it appeared that for memo typeOf actually returns Component.type.$$typeof. Not sure isMemo would work since most of the is* functions use typeOf.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so it's already as good as it can get.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for now.

My real end-goal is for React to export getComponentName, in which case we won't need to bother with these hacks at all (see 15207 and 14319).

case ForwardRef:
return getWrappedName(Component, Component.render, 'ForwardRef');
case Memo:
return getWrappedName(Component, Component.type, 'memo');
default:
return undefined;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/material-ui-utils/src/getDisplayName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ describe('utils/getDisplayName.js', () => {
));
NamedForwardRefComponent.displayName = 'Div';

const AnonymousMemoComponent = React.memo((props, ref) => <div {...props} ref={ref} />);

const MemoComponent = React.memo(function Div(props, ref) {
return <div {...props} ref={ref} />;
});

const NamedMemoComponent = React.memo((props, ref) => <div {...props} ref={ref} />);
NamedMemoComponent.displayName = 'Div';

assert.strictEqual(getDisplayName(SomeComponent), 'SomeComponent');
assert.strictEqual(getDisplayName(SomeOtherComponent), 'CustomDisplayName');
assert.strictEqual(getDisplayName(YetAnotherComponent), 'YetAnotherComponent');
Expand All @@ -48,6 +57,9 @@ describe('utils/getDisplayName.js', () => {
assert.strictEqual(getDisplayName(AnonymousForwardRefComponent), 'ForwardRef');
assert.strictEqual(getDisplayName(ForwardRefComponent), 'ForwardRef(Div)');
assert.strictEqual(getDisplayName(NamedForwardRefComponent), 'Div');
assert.strictEqual(getDisplayName(AnonymousMemoComponent), 'memo');
assert.strictEqual(getDisplayName(MemoComponent), 'memo(Div)');
assert.strictEqual(getDisplayName(NamedMemoComponent), 'Div');
assert.strictEqual(getDisplayName(), undefined);
assert.strictEqual(getDisplayName({}), undefined);
assert.strictEqual(getDisplayName(false), undefined);
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const commonjsOptions = {
'isFragment',
'isLazy',
'isMemo',
'Memo',
'isValidElementType',
],
},
Expand Down