Skip to content

Commit 0c57f48

Browse files
fix(overmind-react): detect unmounted component and force SSR when running in Node
1 parent ecf0424 commit 0c57f48

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/node_modules/overmind-devtools-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Overmind Devtools VSCode",
44
"description": "Devtools for Overmind",
55
"publisher": "christianalfoni",
6-
"version": "0.0.16",
6+
"version": "0.0.17",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/cerebral/overmind.git"

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import {
2424
} from 'react'
2525

2626
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
27+
const IS_TEST = process.env.NODE_ENV === 'test'
28+
const isNode =
29+
!IS_TEST && process && process.title && process.title.includes('node')
2730

2831
export type IReactComponent<P = any> =
2932
| StatelessComponent<P>
@@ -81,8 +84,19 @@ export const createHook = <Config extends IConfiguration>(
8184
}
8285
const useForceRerender = () => {
8386
const [, setState] = useState(() => true)
87+
const mountedRef = useRef(true)
88+
89+
useEffect(
90+
() => () => {
91+
mountedRef.current = false
92+
},
93+
[]
94+
)
95+
8496
const forceRerender = (_, __, flushId): void => {
85-
setState(flushId)
97+
if (mountedRef.current) {
98+
setState(flushId)
99+
}
86100
}
87101
return forceRerender
88102
}
@@ -98,22 +112,14 @@ export const createHook = <Config extends IConfiguration>(
98112
)
99113
}
100114

101-
if ((overmind as any).mode.mode === MODE_SSR) {
115+
if (isNode || (overmind as any).mode.mode === MODE_SSR) {
102116
return {
103117
state: overmind.state,
104118
actions: overmind.actions,
105119
effects: overmind.effects,
106120
addMutationListener: overmind.addMutationListener,
107121
}
108122
}
109-
110-
const component = useCurrentComponent()
111-
const name = component.name
112-
component.__componentId =
113-
typeof component.__componentId === 'undefined'
114-
? nextComponentId++
115-
: component.__componentId
116-
117123
const { current: tree } = useRef<any>(
118124
(overmind as any).proxyStateTree.getTrackStateTree()
119125
)
@@ -132,6 +138,13 @@ export const createHook = <Config extends IConfiguration>(
132138

133139
useLayoutEffect(() => tree.stopTracking())
134140
} else {
141+
const component = useCurrentComponent()
142+
const name = component.name
143+
component.__componentId =
144+
typeof component.__componentId === 'undefined'
145+
? nextComponentId++
146+
: component.__componentId
147+
135148
const { current: componentInstanceId } = useRef<any>(
136149
currentComponentInstanceId++
137150
)

0 commit comments

Comments
 (0)