Skip to content

Commit bed0bb2

Browse files
committed
refactor(react-hot-loader): simplify code
1 parent d944a89 commit bed0bb2

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

packages/react-hot-loader/src/hot.dev.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React, { Component } from 'react'
22
import hoistNonReactStatic from 'hoist-non-react-statics'
3+
import { getComponentDisplayName } from './internal/reactUtils'
34
import AppContainer from './AppContainer.dev'
45
import reactHotLoader from './reactHotLoader'
56

6-
const getDisplayName = WrappedComponent =>
7-
WrappedComponent.displayName || WrappedComponent.name || 'Component'
8-
97
const createHoc = (SourceComponent, TargetComponent) => {
108
hoistNonReactStatic(TargetComponent, SourceComponent)
11-
TargetComponent.displayName = `HotExported${getDisplayName(SourceComponent)}`
9+
TargetComponent.displayName = `HotExported${getComponentDisplayName(
10+
SourceComponent,
11+
)}`
1212
return TargetComponent
1313
}
1414

@@ -43,7 +43,7 @@ const hot = sourceModule => {
4343
// register proxy for wrapped component
4444
reactHotLoader.register(
4545
WrappedComponent,
46-
getDisplayName(WrappedComponent),
46+
getComponentDisplayName(WrappedComponent),
4747
`RHL${sourceModule.id}`,
4848
)
4949

packages/react-hot-loader/src/internal/getReactStack.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ function hydrateTree(root) {
6363
return stack
6464
}
6565

66-
export default function getReactStack(instance) {
66+
function getReactStack(instance) {
6767
const root = getReactInstance(instance)
6868
if (typeof root.tag !== 'number') {
6969
// Traverse stack-based React tree.
7070
return hydrateStack(instance)
7171
}
7272
return hydrateTree(root)
7373
}
74+
75+
export default getReactStack

packages/react-hot-loader/src/internal/reactUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-underscore-dangle */
22

33
export const getComponentDisplayName = type =>
4-
type.displayName || type.name || 'Unknown'
4+
type.displayName || type.name || 'Component'
55

66
export const getReactInstance = instance =>
77
instance._reactInternalFiber ||

packages/react-hot-loader/src/reactHotLoader.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@ function resolveType(type, disableComponentProxy) {
1212
return type
1313
}
1414

15-
// always could...
16-
const couldWrapWithProxy = true
17-
1815
// is proxing is disabled - do not create auto proxies, but use the old ones
19-
const proxy =
20-
!disableComponentProxy && couldWrapWithProxy
21-
? createProxyForType(type)
22-
: getProxyByType(type)
16+
const proxy = disableComponentProxy
17+
? getProxyByType(type)
18+
: createProxyForType(type)
2319

24-
if (!proxy) {
25-
return type
26-
}
27-
28-
return proxy.get()
20+
return proxy ? proxy.get() : type
2921
}
3022

3123
const reactHotLoader = {
@@ -41,14 +33,11 @@ const reactHotLoader = {
4133
if (typeof uniqueLocalName !== 'string' || typeof fileName !== 'string') {
4234
return
4335
}
44-
const id = fileName + '#' + uniqueLocalName // eslint-disable-line prefer-template
4536

46-
updateProxyById(id, type)
37+
updateProxyById(`${fileName}#${uniqueLocalName}`, type)
4738
},
4839

49-
reset(useWeakMap) {
50-
resetProxies(useWeakMap)
51-
},
40+
reset: resetProxies,
5241

5342
patch(React) {
5443
if (!React.createElement.isPatchedByReactHotLoader) {

0 commit comments

Comments
 (0)