1
1
import React , { useMemo , useEffect , Suspense } from 'react' ;
2
- import { Switch , Route , Redirect , useLocation , useHistory } from 'react-router-dom' ;
2
+ import { Routes , Route , useLocation , useNavigate , Navigate } from 'react-router-dom' ;
3
3
import { config , packageVersion } from 'site-desktop-shared' ;
4
4
5
5
import { isMobile } from '../common' ;
@@ -10,23 +10,24 @@ import './index.less';
10
10
11
11
const App = ( ) => {
12
12
const { pathname } = useLocation ( ) ;
13
- const history = useHistory ( ) ;
13
+ const navigate = useNavigate ( ) ;
14
+
15
+ const handleMessage = ( event ) => {
16
+ if ( event . data . pathname && pathname !== event . data . pathname ) {
17
+ navigate ( event . data . pathname ) ;
18
+ }
19
+ } ;
14
20
15
21
useEffect ( ( ) => {
16
22
if ( isMobile ) {
17
23
window . location . replace ( `mobile.html${ window . location . hash } ` ) ;
18
24
}
25
+ return ( ) => {
26
+ window . removeEventListener ( 'message' , handleMessage ) ;
27
+ } ;
19
28
} , [ ] ) ;
20
29
21
- window . addEventListener (
22
- 'message' ,
23
- ( event ) => {
24
- if ( event . data . pathname && pathname !== event . data . pathname ) {
25
- history . push ( event . data . pathname ) ;
26
- }
27
- } ,
28
- false ,
29
- ) ;
30
+ window . addEventListener ( 'message' , handleMessage , false ) ;
30
31
31
32
const path = window . location . pathname . replace ( / \/ i n d e x ( \. h t m l ) ? / , '/' ) ;
32
33
const simulator = `${ path } mobile.html${ window . location . hash } ` ;
@@ -95,20 +96,27 @@ const App = () => {
95
96
currentComponentName = { currentComponentName }
96
97
>
97
98
< Suspense fallback = { < div style = { { height : '110vh' } } > </ div > } >
98
- < Switch >
99
+ < Routes >
99
100
{ routes . map ( ( route ) =>
100
101
route . redirect ? (
101
- < Redirect key = { route . path } to = { route . redirect ( pathname ) } />
102
+ < Route
103
+ key = { route . path }
104
+ path = { route . path }
105
+ element = { < Navigate to = { route . redirect ( pathname ) } replace /> }
106
+ />
102
107
) : (
103
108
< Route
104
109
key = { route . path }
105
110
exact = { route . exact }
106
111
path = { route . path }
107
- render = { ( props ) => < route . component { ...props } routes = { route . routes } /> }
112
+ element = { route . component }
113
+ // render={(props) => (
114
+ // <route.component {...props} element={route.component} routes={route.routes} />
115
+ // )}
108
116
/>
109
117
) ,
110
118
) }
111
- </ Switch >
119
+ </ Routes >
112
120
</ Suspense >
113
121
</ Doc >
114
122
) ;
0 commit comments