@@ -2,7 +2,7 @@ import { RuntimeReactContext } from '@meta/runtime';
2
2
import Garfish , { type interfaces } from 'garfish' ;
3
3
// The loading logic of the current component refers to react-loadable https://github.com/jamiebuilds/react-loadable
4
4
import type React from 'react' ;
5
- import { useContext , useEffect , useRef , useState } from 'react' ;
5
+ import { useContext , useEffect , useMemo , useRef , useState } from 'react' ;
6
6
import { generateSubAppContainerKey , logger } from '../../util' ;
7
7
import { Loadable , type MicroProps } from '../loadable' ;
8
8
// import Loadable from 'react-loadable';
@@ -43,8 +43,6 @@ function getAppInstance(
43
43
appInfo : ModulesInfo [ number ] ,
44
44
manifest ?: Manifest ,
45
45
) {
46
- let locationHref = '' ;
47
-
48
46
// Create a callback registry center
49
47
// This object is within the closure of getAppInstance and will only be created once for the same sub-app.
50
48
// It will be shared by all MicroApp component instances to store the state setter of the currently active component
@@ -56,6 +54,8 @@ function getAppInstance(
56
54
57
55
function MicroApp ( props : MicroProps ) {
58
56
const appRef = useRef < interfaces . App | null > ( null ) ;
57
+ const locationHrefRef = useRef ( '' ) ;
58
+
59
59
const domId = generateSubAppContainerKey ( appInfo ) ;
60
60
const [ { component : SubModuleComponent } , setSubModuleComponent ] =
61
61
useState < {
@@ -135,16 +135,27 @@ or directly pass the "basename":
135
135
basename = props . basename ;
136
136
}
137
137
138
+ const locationPathname = useMemo (
139
+ function ( ) {
140
+ return location ? location . pathname : '' ;
141
+ } ,
142
+ [ location ? location . pathname : '' ] ,
143
+ ) ;
144
+
138
145
// useLocation is NECESSARY in syncPopStateEvent
139
146
useEffect ( ( ) => {
140
- if ( location && locationHref !== location . pathname && ! Garfish . running ) {
141
- locationHref = location . pathname ;
147
+ if (
148
+ location &&
149
+ locationHrefRef . current !== location . pathname &&
150
+ ! Garfish . running
151
+ ) {
152
+ locationHrefRef . current = location . pathname ;
142
153
const popStateEvent = new PopStateEvent ( 'popstate' ) ;
143
154
( popStateEvent as any ) . garfish = true ;
144
155
dispatchEvent ( popStateEvent ) ;
145
156
logger ( `MicroApp Garfish.loadApp popstate` ) ;
146
157
}
147
- } , [ location ] ) ;
158
+ } , [ locationPathname ] ) ;
148
159
149
160
useEffect ( ( ) => {
150
161
// [MODIFIED] Register the current instance's state setter when the component mounts
0 commit comments