@@ -44,6 +44,16 @@ function getAppInstance(
44
44
manifest ?: Manifest ,
45
45
) {
46
46
let locationHref = '' ;
47
+
48
+ // Create a callback registry center
49
+ // This object is within the closure of getAppInstance and will only be created once for the same sub-app.
50
+ // It will be shared by all MicroApp component instances to store the state setter of the currently active component
51
+ const componentSetterRegistry = {
52
+ current : null as React . Dispatch <
53
+ React . SetStateAction < { component : React . ComponentType < any > | null } >
54
+ > | null ,
55
+ } ;
56
+
47
57
function MicroApp ( props : MicroProps ) {
48
58
const appRef = useRef < interfaces . App | null > ( null ) ;
49
59
const domId = generateSubAppContainerKey ( appInfo ) ;
@@ -137,6 +147,9 @@ or directly pass the "basename":
137
147
} , [ location ] ) ;
138
148
139
149
useEffect ( ( ) => {
150
+ // [MODIFIED] Register the current instance's state setter when the component mounts
151
+ componentSetterRegistry . current = setSubModuleComponent ;
152
+
140
153
const { setLoadingState, ...userProps } = props ;
141
154
142
155
const loadAppOptions : Omit < interfaces . AppInfo , 'name' > = {
@@ -164,7 +177,15 @@ or directly pass the "basename":
164
177
return {
165
178
mount : ( ...props ) => {
166
179
if ( componetRenderMode && SubComponent ) {
167
- setSubModuleComponent ( { component : SubComponent } ) ;
180
+ // [MODIFIED] Get and call the current state setter from the registry center
181
+ // This way, even if the mount method is cached, it can still call the setter of the latest component instance
182
+ if ( componentSetterRegistry . current ) {
183
+ componentSetterRegistry . current ( { component : SubComponent } ) ;
184
+ } else {
185
+ logger (
186
+ `[Garfish] MicroApp for "${ appInfo . name } " tried to mount, but no active component setter was found.` ,
187
+ ) ;
188
+ }
168
189
return undefined ;
169
190
} else {
170
191
logger ( 'MicroApp customer render' , props ) ;
@@ -230,7 +251,11 @@ or directly pass the "basename":
230
251
}
231
252
}
232
253
renderApp ( ) ;
254
+
233
255
return ( ) => {
256
+ // [MODIFIED] Unregister the state setter when the component unmounts to prevent memory leaks and calls to unmounted components
257
+ componentSetterRegistry . current = null ;
258
+
234
259
if ( appRef . current ) {
235
260
const { appInfo } = appRef . current ;
236
261
if ( appInfo . cache ) {
0 commit comments