Skip to content

Commit 3a1edc6

Browse files
fix(overmind-react): fix issues with hook
1 parent b2802d7 commit 3a1edc6

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

packages/node_modules/overmind-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"devDependencies": {
4545
"@types/jest": "^23.1.4",
46-
"@types/react": "^16.4.6",
46+
"@types/react": "^16.7.13",
4747
"jest": "^23.5.0",
4848
"react": "^16.7.0-alpha.1",
4949
"react-test-renderer": "^16.4.1",

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

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,58 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
5050
typeof component.__componentId === 'undefined'
5151
? nextComponentId++
5252
: component.__componentId
53-
const [thisComponentInstanceId] = useState(() => componentInstanceId++)
54-
const [_, forceUpdate] = useState(null)
55-
53+
const [overmindState, setOvermindState] = useState<any>(null)
54+
const trackId = overmind.trackState()
5655
useEffect(() => {
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, {
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, {
6581
componentId: component.__componentId,
6682
componentInstanceId: thisComponentInstanceId,
6783
name,
84+
paths: Array.from(paths),
6885
})
6986
}
70-
}, [])
71-
72-
const trackId = overmind.trackState()
73-
74-
useEffect(() => {
75-
const paths = overmind.clearTrackState(trackId)
87+
})
88+
useEffect(
89+
() => {
90+
return () => {
91+
if (!overmindState) {
92+
return
93+
}
7694

77-
if (paths && paths.size) {
78-
const listener = overmind.addFlushListener(paths, (flushId) => {
79-
forceUpdate(flushId)
80-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
95+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
8196
componentId: component.__componentId,
82-
componentInstanceId: thisComponentInstanceId,
97+
componentInstanceId: overmindState.componentInstanceId,
8398
name,
84-
flushId,
85-
paths: Array.from(paths),
8699
})
87-
})
88-
return () => {
89-
listener.dispose()
100+
overmindState.listener.dispose()
90101
}
91-
}
92-
})
93-
102+
},
103+
[overmindState]
104+
)
94105
return overmind
95106
}
96107
}

0 commit comments

Comments
 (0)