Skip to content

Commit 8df1180

Browse files
Merge pull request cerebral#166 from garth/fix-create-hook-unmount
fix(overmind-react): refactor createHook with component lifecycle fixes
2 parents 5d3d9be + cea7933 commit 8df1180

File tree

1 file changed

+31
-38
lines changed
  • packages/node_modules/overmind-react/src

1 file changed

+31
-38
lines changed

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

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,54 +50,47 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
5050
typeof component.__componentId === 'undefined'
5151
? nextComponentId++
5252
: component.__componentId
53-
const [overmindState, setOvermindState] = useState(null)
54-
const trackId = overmind.trackState()
53+
const [thisComponentInstanceId] = useState(() => componentInstanceId++)
54+
const [_, forceUpdate] = useState(null)
55+
5556
useEffect(() => {
56-
const paths = overmind.clearTrackState(trackId)
57-
if (overmindState) {
58-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
59-
componentId: component.__componentId,
60-
componentInstanceId: overmindState.componentInstanceId,
61-
name,
62-
paths: Array.from(paths),
63-
})
64-
overmindState.listener.update(paths)
65-
} else {
66-
const thisComponentInstanceId = componentInstanceId++
67-
setOvermindState({
68-
componentInstanceId: thisComponentInstanceId,
69-
listener: overmind.addFlushListener(paths, (flushId) => {
70-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
71-
componentId: component.__componentId,
72-
componentInstanceId: thisComponentInstanceId,
73-
name,
74-
flushId,
75-
paths: Array.from(paths),
76-
})
77-
setOvermindState((state) => state)
78-
}),
79-
})
80-
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
57+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
58+
componentId: component.__componentId,
59+
componentInstanceId: thisComponentInstanceId,
60+
name,
61+
paths: [],
62+
})
63+
return () => {
64+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
8165
componentId: component.__componentId,
8266
componentInstanceId: thisComponentInstanceId,
8367
name,
84-
paths: Array.from(paths),
8568
})
8669
}
87-
})
88-
useEffect(
89-
() => {
90-
return () => {
91-
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
70+
}, [])
71+
72+
const trackId = overmind.trackState()
73+
74+
useEffect(() => {
75+
const paths = overmind.clearTrackState(trackId)
76+
77+
if (paths && paths.size) {
78+
const listener = overmind.addFlushListener(paths, (flushId) => {
79+
forceUpdate(flushId)
80+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
9281
componentId: component.__componentId,
93-
componentInstanceId: overmindState.componentInstanceId,
82+
componentInstanceId: thisComponentInstanceId,
9483
name,
84+
flushId,
85+
paths: Array.from(paths),
9586
})
96-
overmindState.listener.dispose()
87+
})
88+
return () => {
89+
listener.dispose()
9790
}
98-
},
99-
[component.__componentId]
100-
)
91+
}
92+
})
93+
10194
return overmind
10295
}
10396
}

0 commit comments

Comments
 (0)