From 7b8a4ea234d67fd4e8fddba9ee53df36cd70adb7 Mon Sep 17 00:00:00 2001 From: Henrik Date: Mon, 19 Jun 2017 15:03:02 +0200 Subject: [PATCH] Create displayName util (#2286) --- lib/dynamic.js | 3 ++- lib/side-effect.js | 5 +---- lib/utils.js | 6 +++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/dynamic.js b/lib/dynamic.js index 7e4841f1..73052e53 100644 --- a/lib/dynamic.js +++ b/lib/dynamic.js @@ -1,4 +1,5 @@ import React from 'react' +import { getDisplayName } from './utils' let currentChunks = new Set() @@ -55,7 +56,7 @@ export default function dynamicComponent (p, o) { loadComponent () { promise.then((AsyncComponent) => { // Set a readable displayName for the wrapper component - const asyncCompName = AsyncComponent.displayName || AsyncComponent.name + const asyncCompName = getDisplayName(AsyncComponent) if (asyncCompName) { DynamicComponent.displayName = `DynamicComponent for ${asyncCompName}` } diff --git a/lib/side-effect.js b/lib/side-effect.js index 164ea73f..954e4e68 100644 --- a/lib/side-effect.js +++ b/lib/side-effect.js @@ -1,4 +1,5 @@ import React, { Component } from 'react' +import { getDisplayName } from './utils' export default function withSideEffect (reduceComponentsToState, handleStateChangeOnClient, mapStateOnServer) { if (typeof reduceComponentsToState !== 'function') { @@ -13,10 +14,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan throw new Error('Expected mapStateOnServer to either be undefined or a function.') } - function getDisplayName (WrappedComponent) { - return WrappedComponent.displayName || WrappedComponent.name || 'Component' - } - return function wrap (WrappedComponent) { if (typeof WrappedComponent !== 'function') { throw new Error('Expected WrappedComponent to be a React component.') diff --git a/lib/utils.js b/lib/utils.js index 2831c0a4..5dcaaffa 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -42,12 +42,16 @@ export function printAndExit (message, code = 1) { process.exit(code) } +export function getDisplayName (Component) { + return Component.displayName || Component.name || 'Unknown' +} + export async function loadGetInitialProps (Component, ctx) { if (!Component.getInitialProps) return {} const props = await Component.getInitialProps(ctx) if (!props && (!ctx.res || !ctx.res.finished)) { - const compName = Component.displayName || Component.name + const compName = getDisplayName(Component) const message = `"${compName}.getInitialProps()" should resolve to an object. But found "${props}" instead.` throw new Error(message) }