Skip to content

Commit

Permalink
Fix the displayName of HOC components
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC committed Mar 22, 2019
1 parent 61c4645 commit cfc7073
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ export function hasLoadedNamespace(ns, i18n) {

return false;
}

export function getDisplayName(Component) {
return (
Component.displayName ||
Component.name ||
(typeof Component === 'string' && Component.length > 0 ? Component : 'Unknown')
);
}
2 changes: 2 additions & 0 deletions src/withSSR.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { useSSR } from './useSSR';
import { composeInitialProps } from './context';
import { getDisplayName } from './utils';

export function withSSR() {
return function Extend(WrappedComponent) {
Expand All @@ -13,6 +14,7 @@ export function withSSR() {
}

I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent);
I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`;

return I18nextWithSSR;
};
Expand Down
5 changes: 5 additions & 0 deletions src/withTranslation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { useTranslation } from './useTranslation';
import { getDisplayName } from './utils';

export function withTranslation(ns) {
return function Extend(WrappedComponent) {
Expand All @@ -14,6 +15,10 @@ export function withTranslation(ns) {
});
}

I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(
WrappedComponent,
)})`;

return I18nextWithTranslation;
};
}

0 comments on commit cfc7073

Please sign in to comment.