Skip to content

Commit be84911

Browse files
Merge pull request cerebral#409 from darkowic/406-displayName-in-devtools
fix(react): fix display name in devtools for react cerebral#406
2 parents 8584e58 + d7cc533 commit be84911

File tree

1 file changed

+24
-5
lines changed
  • packages/node_modules/overmind-react/src

1 file changed

+24
-5
lines changed

packages/node_modules/overmind-react/src/index.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ type Omit<T, K extends keyof T> = Pick<
2727
{ [P in K]: never } & { [x: string]: never; [x: number]: never })[keyof T]
2828
>
2929

30+
function getFiberType(component) {
31+
if (component.type) {
32+
// React.memo
33+
return getFiberType(component.type)
34+
}
35+
// React.forwardRef
36+
return component.render || component
37+
}
38+
39+
// Inspired from https://github.com/facebook/react/blob/master/packages/react-devtools-shared/src/backend/renderer.js
40+
function getDisplayName(component): string {
41+
const type = getFiberType(component);
42+
return (
43+
type.displayName ||
44+
type.name ||
45+
'Anonymous'
46+
);
47+
}
48+
3049
export interface IConnect<Config extends IConfiguration> {
3150
overmind: {
3251
state: Overmind<Config>['state']
@@ -49,8 +68,8 @@ const context = react.createContext<Overmind<IConfiguration>>({} as Overmind<
4968
>)
5069
let nextComponentId = 0
5170

52-
export const Provider: React.ProviderExoticComponent<
53-
React.ProviderProps<Overmind<IConfiguration> | OvermindMock<IConfiguration>>
71+
export const Provider: react.ProviderExoticComponent<
72+
react.ProviderProps<Overmind<IConfiguration> | OvermindMock<IConfiguration>>
5473
> = context.Provider
5574

5675
function useForceRerender() {
@@ -137,7 +156,7 @@ const useState = <Config extends IConfiguration>(): Overmind<Config>['state'] =
137156
})
138157
} else {
139158
const component = useCurrentComponent()
140-
const name = component.name
159+
const name = getDisplayName(component)
141160
component.__componentId =
142161
typeof component.__componentId === 'undefined'
143162
? nextComponentId++
@@ -284,7 +303,7 @@ export const createConnect: <ThisConfig extends IConfiguration>() => <Props>(com
284303
component
285304
)=> {
286305
let componentInstanceId = 0
287-
const name = component.name
306+
const name = component.displayName || component.name || 'Anonymous';
288307
const populatedComponent = component as any
289308
populatedComponent.__componentId =
290309
typeof populatedComponent.__componentId === 'undefined'
@@ -470,7 +489,7 @@ export const createConnect: <ThisConfig extends IConfiguration>() => <Props>(com
470489

471490
Object.defineProperties(HOC, {
472491
name: {
473-
value: 'Connect' + (component.displayName || component.name || ''),
492+
value: 'Connect' + name,
474493
},
475494
})
476495

0 commit comments

Comments
 (0)