From cfc7073c30b363bfb75e8a6838b5f5677e87b883 Mon Sep 17 00:00:00 2001 From: Simeon Cheeseman Date: Fri, 22 Mar 2019 17:40:57 +0900 Subject: [PATCH] Fix the displayName of HOC components --- src/utils.js | 8 ++++++++ src/withSSR.js | 2 ++ src/withTranslation.js | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/utils.js b/src/utils.js index 538e84a87..75540025d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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') + ); +} diff --git a/src/withSSR.js b/src/withSSR.js index a1b4464fb..b8071766c 100644 --- a/src/withSSR.js +++ b/src/withSSR.js @@ -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) { @@ -13,6 +14,7 @@ export function withSSR() { } I18nextWithSSR.getInitialProps = composeInitialProps(WrappedComponent); + I18nextWithSSR.displayName = `withI18nextSSR(${getDisplayName(WrappedComponent)})`; return I18nextWithSSR; }; diff --git a/src/withTranslation.js b/src/withTranslation.js index 5e708232a..9b4e3a059 100644 --- a/src/withTranslation.js +++ b/src/withTranslation.js @@ -1,5 +1,6 @@ import React from 'react'; import { useTranslation } from './useTranslation'; +import { getDisplayName } from './utils'; export function withTranslation(ns) { return function Extend(WrappedComponent) { @@ -14,6 +15,10 @@ export function withTranslation(ns) { }); } + I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName( + WrappedComponent, + )})`; + return I18nextWithTranslation; }; }