Skip to content

Commit 54b3f78

Browse files
fix(overmind-react): fix double render with hook
1 parent a40a191 commit 54b3f78

File tree

1 file changed

+30
-33
lines changed
  • packages/node_modules/overmind-react/src

1 file changed

+30
-33
lines changed

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

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
useEffect,
1212
// @ts-ignore
1313
useState,
14+
useLayoutEffect,
1415
} from 'react'
1516

1617
export type IReactComponent<P = any> =
@@ -52,11 +53,13 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
5253
typeof component.__componentId === 'undefined'
5354
? nextComponentId++
5455
: component.__componentId
55-
const [overmindState, setOvermindState] = useState<any>(null)
56+
const [overmindState, setOvermindState] = useState<any>({})
57+
5658
const trackId = overmind.trackState()
57-
useEffect(() => {
59+
useLayoutEffect(() => {
5860
const paths = overmind.clearTrackState(trackId)
59-
if (overmindState) {
61+
62+
if (overmindState.listener) {
6063
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
6164
componentId: component.__componentId,
6265
componentInstanceId: overmindState.componentInstanceId,
@@ -65,45 +68,39 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
6568
})
6669
overmindState.listener.update(paths)
6770
} else {
68-
const thisComponentInstanceId = componentInstanceId++
69-
setOvermindState({
70-
componentInstanceId: thisComponentInstanceId,
71-
listener: overmind.addFlushListener(paths, (flushId) => {
72-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
73-
componentId: component.__componentId,
74-
componentInstanceId: thisComponentInstanceId,
75-
name,
76-
flushId,
77-
paths: Array.from(paths),
78-
})
79-
setOvermindState((state) => state)
80-
}),
71+
overmindState.componentInstanceId = componentInstanceId++
72+
overmindState.listener = overmind.addFlushListener(paths, (flushId) => {
73+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
74+
componentId: component.__componentId,
75+
componentInstanceId: overmindState.componentInstanceId,
76+
name,
77+
flushId,
78+
paths: Array.from(paths),
79+
})
80+
setOvermindState((state) => state)
8181
})
8282
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
8383
componentId: component.__componentId,
84-
componentInstanceId: thisComponentInstanceId,
84+
componentInstanceId: overmindState.componentInstanceId,
8585
name,
8686
paths: Array.from(paths),
8787
})
8888
}
8989
})
90-
useEffect(
91-
() => {
92-
return () => {
93-
if (!overmindState) {
94-
return
95-
}
96-
97-
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
98-
componentId: component.__componentId,
99-
componentInstanceId: overmindState.componentInstanceId,
100-
name,
101-
})
102-
overmindState.listener.dispose()
90+
useEffect(() => {
91+
return () => {
92+
if (!overmindState.listener) {
93+
return
10394
}
104-
},
105-
[overmindState]
106-
)
95+
96+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
97+
componentId: component.__componentId,
98+
componentInstanceId: overmindState.componentInstanceId,
99+
name,
100+
})
101+
overmindState.listener.dispose()
102+
}
103+
}, [])
107104
return overmind
108105
}
109106
}

0 commit comments

Comments
 (0)