Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b8a4ea

Browse files
HaNdTriXarunoda
authored andcommitted
Create displayName util (vercel#2286)
1 parent fe77397 commit 7b8a4ea

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

lib/dynamic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import { getDisplayName } from './utils'
23

34
let currentChunks = new Set()
45

@@ -55,7 +56,7 @@ export default function dynamicComponent (p, o) {
5556
loadComponent () {
5657
promise.then((AsyncComponent) => {
5758
// Set a readable displayName for the wrapper component
58-
const asyncCompName = AsyncComponent.displayName || AsyncComponent.name
59+
const asyncCompName = getDisplayName(AsyncComponent)
5960
if (asyncCompName) {
6061
DynamicComponent.displayName = `DynamicComponent for ${asyncCompName}`
6162
}

lib/side-effect.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react'
2+
import { getDisplayName } from './utils'
23

34
export default function withSideEffect (reduceComponentsToState, handleStateChangeOnClient, mapStateOnServer) {
45
if (typeof reduceComponentsToState !== 'function') {
@@ -13,10 +14,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
1314
throw new Error('Expected mapStateOnServer to either be undefined or a function.')
1415
}
1516

16-
function getDisplayName (WrappedComponent) {
17-
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
18-
}
19-
2017
return function wrap (WrappedComponent) {
2118
if (typeof WrappedComponent !== 'function') {
2219
throw new Error('Expected WrappedComponent to be a React component.')

lib/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ export function printAndExit (message, code = 1) {
4242
process.exit(code)
4343
}
4444

45+
export function getDisplayName (Component) {
46+
return Component.displayName || Component.name || 'Unknown'
47+
}
48+
4549
export async function loadGetInitialProps (Component, ctx) {
4650
if (!Component.getInitialProps) return {}
4751

4852
const props = await Component.getInitialProps(ctx)
4953
if (!props && (!ctx.res || !ctx.res.finished)) {
50-
const compName = Component.displayName || Component.name
54+
const compName = getDisplayName(Component)
5155
const message = `"${compName}.getInitialProps()" should resolve to an object. But found "${props}" instead.`
5256
throw new Error(message)
5357
}

0 commit comments

Comments
 (0)