1+ import 'proxy-state-tree'
2+
13import {
4+ EventType ,
25 IConfiguration ,
36 MODE_SSR ,
4- EventType ,
57 Overmind ,
68 OvermindMock ,
79} from 'overmind'
8- import 'proxy-state-tree'
9- import {
10- // @ts -ignore
11- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED ,
12- ClassicComponentClass ,
13- Component ,
14- ComponentClass ,
15- createElement ,
16- StatelessComponent ,
17- // @ts -ignore
18- useEffect ,
19- // @ts -ignore
20- useState ,
21- useRef ,
22- useLayoutEffect ,
23- createContext ,
24- useContext ,
25- } from 'react'
2610import { IMutationCallback } from 'proxy-state-tree'
11+ import * as react from 'react'
2712
2813const IS_PRODUCTION = process . env . NODE_ENV === 'production'
2914const IS_TEST = process . env . NODE_ENV === 'test'
3015const isNode =
3116 ! IS_TEST && process && process . title && process . title . includes ( 'node' )
3217
3318export type IReactComponent < P = any > =
34- | StatelessComponent < P >
35- | ComponentClass < P >
36- | ClassicComponentClass < P >
19+ | react . StatelessComponent < P >
20+ | react . ComponentClass < P >
21+ | react . ClassicComponentClass < P >
3722
3823// Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766
3924type Omit < T , K extends keyof T > = Pick <
@@ -52,7 +37,13 @@ export interface IConnect<Config extends IConfiguration> {
5237 }
5338}
5439
55- const context = createContext < Overmind < IConfiguration > > ( { } as Overmind <
40+ function throwMissingContextError ( ) {
41+ throw new Error (
42+ 'The Overmind hook could not find an Overmind instance on the context of React. Please make sure you use the Provider component at the top of your application and expose the Overmind instance there. Please read more in the React guide on the website'
43+ )
44+ }
45+
46+ const context = react . createContext < Overmind < IConfiguration > > ( { } as Overmind <
5647 IConfiguration
5748> )
5849let nextComponentId = 0
@@ -61,24 +52,17 @@ export const Provider: React.ProviderExoticComponent<
6152 React . ProviderProps < Overmind < IConfiguration > | OvermindMock < IConfiguration > >
6253> = context . Provider
6354
64- export const createHook = < Config extends IConfiguration > (
65- overmindInstance ?: Overmind < Config >
66- ) : ( ( ) => {
55+ export const createHook = < Config extends IConfiguration > ( ) : ( ( ) => {
6756 state : Overmind < Config > [ 'state' ]
6857 actions : Overmind < Config > [ 'actions' ]
6958 effects : Overmind < Config > [ 'effects' ]
7059 addMutationListener : ( cb : IMutationCallback ) => ( ) => void
7160 reaction : Overmind < Config > [ 'reaction' ]
7261} ) => {
73- if ( overmindInstance ) {
74- console . warn (
75- 'DEPRECATION - Do not pass the overmind instance to "createHook", but use the provider. Please read docs for more information'
76- )
77- }
7862 let currentComponentInstanceId = 0
7963 const {
8064 ReactCurrentOwner,
81- } = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
65+ } = ( react as any ) . __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
8266 const useCurrentComponent = ( ) => {
8367 return ReactCurrentOwner &&
8468 ReactCurrentOwner . current &&
@@ -87,10 +71,10 @@ export const createHook = <Config extends IConfiguration>(
8771 : { }
8872 }
8973 const useForceRerender = ( ) => {
90- const [ flushId , setState ] = useState ( ( ) => - 1 )
91- const mountedRef = useRef ( true )
74+ const [ flushId , setState ] = react . useState ( ( ) => - 1 )
75+ const mountedRef = react . useRef ( true )
9276
93- useEffect (
77+ react . useEffect (
9478 ( ) => ( ) => {
9579 mountedRef . current = false
9680 } ,
@@ -109,14 +93,10 @@ export const createHook = <Config extends IConfiguration>(
10993 }
11094
11195 return ( ) => {
112- const overmind = ( overmindInstance || useContext ( context ) ) as Overmind <
113- Config
114- >
96+ const overmind = react . useContext ( context ) as Overmind < Config >
11597
11698 if ( ! overmind ) {
117- throw new Error (
118- 'You are not exposing Overmind on the React context. Please make sure you use the Provider, as shown in the React guide'
119- )
99+ throwMissingContextError ( )
120100 }
121101
122102 if ( isNode || ( overmind as any ) . mode . mode === MODE_SSR ) {
@@ -128,21 +108,21 @@ export const createHook = <Config extends IConfiguration>(
128108 reaction : overmind . reaction ,
129109 }
130110 }
131- const { current : tree } = useRef < any > (
111+ const { current : tree } = react . useRef < any > (
132112 ( overmind as any ) . proxyStateTree . getTrackStateTree ( )
133113 )
134114
135115 if ( IS_PRODUCTION ) {
136116 const { forceRerender } = useForceRerender ( )
137117
138- useEffect (
118+ react . useEffect (
139119 ( ) => ( ) => {
140120 ; ( overmind as any ) . proxyStateTree . disposeTree ( tree )
141121 } ,
142122 [ ]
143123 )
144124
145- useLayoutEffect ( ( ) => tree . stopTracking ( ) )
125+ react . useLayoutEffect ( ( ) => tree . stopTracking ( ) )
146126
147127 tree . track ( forceRerender )
148128 } else {
@@ -153,13 +133,13 @@ export const createHook = <Config extends IConfiguration>(
153133 ? nextComponentId ++
154134 : component . __componentId
155135
156- const { current : componentInstanceId } = useRef < any > (
136+ const { current : componentInstanceId } = react . useRef < any > (
157137 currentComponentInstanceId ++
158138 )
159139
160140 const { flushId, forceRerender } = useForceRerender ( )
161141
162- useLayoutEffect ( ( ) => {
142+ react . useLayoutEffect ( ( ) => {
163143 overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
164144 componentId : component . __componentId ,
165145 componentInstanceId,
@@ -177,7 +157,7 @@ export const createHook = <Config extends IConfiguration>(
177157 }
178158 } , [ ] )
179159
180- useLayoutEffect ( ( ) => {
160+ react . useLayoutEffect ( ( ) => {
181161 tree . stopTracking ( )
182162 overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
183163 componentId : component . __componentId ,
@@ -200,9 +180,7 @@ export const createHook = <Config extends IConfiguration>(
200180 }
201181}
202182
203- export const createConnect = < ThisConfig extends IConfiguration > (
204- overmindInstance ?: Overmind < ThisConfig >
205- ) => {
183+ export const createConnect = < ThisConfig extends IConfiguration > ( ) => {
206184 return < Props > (
207185 component : IReactComponent <
208186 Props & {
@@ -219,12 +197,6 @@ export const createConnect = <ThisConfig extends IConfiguration>(
219197 keyof IConnect < Overmind < ThisConfig > >
220198 >
221199 > => {
222- if ( overmindInstance ) {
223- console . warn (
224- 'DEPRECATION - Do not pass the overmind instance to "createConnect", but use the provider. Please read docs for more information'
225- )
226- }
227-
228200 let componentInstanceId = 0
229201 const name = component . name
230202 const populatedComponent = component as any
@@ -250,7 +222,7 @@ export const createConnect = <ThisConfig extends IConfiguration>(
250222 }
251223
252224 if ( IS_PRODUCTION ) {
253- class HOC extends Component {
225+ class HOC extends react . Component {
254226 tree : any
255227 overmind : any
256228 state : {
@@ -260,7 +232,11 @@ export const createConnect = <ThisConfig extends IConfiguration>(
260232 static contextType = context
261233 constructor ( props , context ) {
262234 super ( props )
263- this . overmind = overmindInstance || context
235+
236+ if ( ! context ) {
237+ throwMissingContextError ( )
238+ }
239+ this . overmind = context
264240 this . tree = this . overmind . proxyStateTree . getTrackStateTree ( )
265241 this . state = {
266242 overmind : {
@@ -297,13 +273,13 @@ export const createConnect = <ThisConfig extends IConfiguration>(
297273 }
298274 render ( ) {
299275 if ( isClassComponent ) {
300- return createElement ( component , {
276+ return react . createElement ( component , {
301277 ...this . props ,
302278 overmind : this . state . overmind ,
303279 } as any )
304280 }
305281
306- return createElement ( this . wrappedComponent , {
282+ return react . createElement ( this . wrappedComponent , {
307283 ...this . props ,
308284 overmind : this . state . overmind ,
309285 } as any )
@@ -312,7 +288,7 @@ export const createConnect = <ThisConfig extends IConfiguration>(
312288
313289 return HOC as any
314290 } else {
315- class HOC extends Component {
291+ class HOC extends react . Component {
316292 tree : any
317293 overmind : any
318294 componentInstanceId = componentInstanceId ++
@@ -325,7 +301,12 @@ export const createConnect = <ThisConfig extends IConfiguration>(
325301 static contextType = context
326302 constructor ( props , context ) {
327303 super ( props )
328- this . overmind = overmindInstance || context
304+
305+ if ( ! context ) {
306+ throwMissingContextError ( )
307+ }
308+
309+ this . overmind = context
329310 this . tree = this . overmind . proxyStateTree . getTrackStateTree ( )
330311 this . state = {
331312 overmind : {
@@ -389,12 +370,12 @@ export const createConnect = <ThisConfig extends IConfiguration>(
389370 }
390371 render ( ) {
391372 if ( isClassComponent ) {
392- return createElement ( component , {
373+ return react . createElement ( component , {
393374 ...this . props ,
394375 overmind : this . state . overmind ,
395376 } as any )
396377 }
397- return createElement ( this . wrappedComponent , {
378+ return react . createElement ( this . wrappedComponent , {
398379 ...this . props ,
399380 overmind : this . state . overmind ,
400381 } as any )
0 commit comments