Skip to content

Commit 237a190

Browse files
feat(overmind-react): use internal hook to reference component in hook
1 parent 980fc62 commit 237a190

File tree

1 file changed

+18
-15
lines changed
  • packages/node_modules/overmind-react/src

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { BaseApp, EventType, Overmind, Configuration } from 'overmind'
22
import * as React from 'react'
3-
// @ts-ignore
4-
import { useEffect, useState } from 'react'
3+
import {
4+
// @ts-ignore
5+
useEffect,
6+
// @ts-ignore
7+
useState,
8+
// @ts-ignore
9+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
10+
} from 'react'
511

612
export type IReactComponent<P = any> =
713
| React.StatelessComponent<P>
@@ -29,21 +35,18 @@ export type TConnect<App extends BaseApp> = {
2935

3036
let nextComponentId = 0
3137

32-
type FunctionComponent = {
33-
(...args: any[]): React.ReactChildren
34-
__componentId: number
35-
displayName: string
36-
}
37-
3838
export const createHook = <App extends Overmind<Configuration>>(app: App) => {
3939
let componentInstanceId = 0
40-
return (component: FunctionComponent) => {
41-
if (!component) {
42-
throw new Error(
43-
'Overmind - You have to the Overmind hook the component reference, for debugging purposes'
44-
)
45-
}
46-
const name = component.name || component.displayName
40+
const {
41+
ReactCurrentOwner,
42+
} = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
43+
const useCurrentComponent = () => {
44+
return ReactCurrentOwner.current.elementType
45+
}
46+
47+
return () => {
48+
const component = useCurrentComponent()
49+
const name = component.name
4750
component.__componentId =
4851
typeof component.__componentId === 'undefined'
4952
? nextComponentId++

0 commit comments

Comments
 (0)