1
1
import React , { useRef } from 'react' ;
2
2
3
- import { createMemoryHistory , MemoryHistoryBuildOptions } from 'history' ;
3
+ import { createMemoryHistory } from 'history' ;
4
4
5
5
import { Router } from '../router' ;
6
- import { RouterProps } from '../router/types' ;
7
6
8
7
import { MemoryRouterProps } from '../../common/types' ;
9
8
10
- const getRouterProps = ( memoryRouterProps : MemoryRouterProps ) => {
11
- const {
12
- isStatic = false ,
13
- isGlobal = true ,
14
- basePath,
15
- routes,
16
- resourceData,
17
- resourceContext,
18
- } = memoryRouterProps ;
19
- let routerProps : Partial < RouterProps > = {
20
- basePath,
21
- routes,
22
- isStatic,
23
- isGlobal,
24
- } ;
25
-
26
- if ( resourceData ) {
27
- routerProps = { ...routerProps , resourceData } ;
28
- }
9
+ const lazy = < T extends any > ( callback : ( ) => T ) => {
10
+ let firstCall = true ;
11
+ let current : T | undefined = undefined ;
29
12
30
- if ( resourceContext ) {
31
- routerProps = { ...routerProps , resourceContext } ;
32
- }
13
+ return ( ) => {
14
+ if ( firstCall ) {
15
+ current = callback ( ) ;
16
+ firstCall = false ;
17
+ }
33
18
34
- return routerProps ;
19
+ return current ;
20
+ } ;
35
21
} ;
36
22
37
23
/**
38
24
* Ensures the router store uses memory history.
39
25
*
40
26
*/
41
- export const MemoryRouter = ( props : MemoryRouterProps ) => {
42
- const { location, children } = props ;
43
-
44
- const newGetHistory = ( ) =>
27
+ export const MemoryRouter = ( {
28
+ isStatic = false ,
29
+ isGlobal = true ,
30
+ location,
31
+ children,
32
+ basePath,
33
+ routes,
34
+ resourceData,
35
+ resourceContext,
36
+ } : MemoryRouterProps ) => {
37
+ const newGetHistory = lazy ( ( ) =>
45
38
createMemoryHistory ( {
46
39
initialEntries : location !== undefined ? [ location ] : undefined ,
47
- } ) ;
40
+ } )
41
+ ) ;
48
42
49
43
const historyState = useRef ( {
50
44
getHistory : newGetHistory ,
@@ -55,13 +49,16 @@ export const MemoryRouter = (props: MemoryRouterProps) => {
55
49
historyState . current . getHistory = newGetHistory ;
56
50
}
57
51
58
- const routerProps = getRouterProps ( props ) ;
59
-
60
52
return (
61
53
// @ts -ignore suppress history will be overwritten warning
62
54
< Router
55
+ isStatic = { isStatic }
56
+ isGlobal = { isGlobal }
63
57
history = { historyState . current . getHistory ( ) }
64
- { ...( routerProps as RouterProps ) }
58
+ basePath = { basePath }
59
+ routes = { routes }
60
+ resourceData = { resourceData }
61
+ resourceContext = { resourceContext }
65
62
>
66
63
{ children }
67
64
</ Router >
0 commit comments