11import React , { useRef } from 'react' ;
22
3- import { createMemoryHistory , MemoryHistoryBuildOptions } from 'history' ;
3+ import { createMemoryHistory } from 'history' ;
44
55import { Router } from '../router' ;
6- import { RouterProps } from '../router/types' ;
76
87import { MemoryRouterProps } from '../../common/types' ;
98
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 ;
2912
30- if ( resourceContext ) {
31- routerProps = { ...routerProps , resourceContext } ;
32- }
13+ return ( ) => {
14+ if ( firstCall ) {
15+ current = callback ( ) ;
16+ firstCall = false ;
17+ }
3318
34- return routerProps ;
19+ return current ;
20+ } ;
3521} ;
3622
3723/**
3824 * Ensures the router store uses memory history.
3925 *
4026 */
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 ( ( ) =>
4538 createMemoryHistory ( {
4639 initialEntries : location !== undefined ? [ location ] : undefined ,
47- } ) ;
40+ } )
41+ ) ;
4842
4943 const historyState = useRef ( {
5044 getHistory : newGetHistory ,
@@ -55,13 +49,16 @@ export const MemoryRouter = (props: MemoryRouterProps) => {
5549 historyState . current . getHistory = newGetHistory ;
5650 }
5751
58- const routerProps = getRouterProps ( props ) ;
59-
6052 return (
6153 // @ts -ignore suppress history will be overwritten warning
6254 < Router
55+ isStatic = { isStatic }
56+ isGlobal = { isGlobal }
6357 history = { historyState . current . getHistory ( ) }
64- { ...( routerProps as RouterProps ) }
58+ basePath = { basePath }
59+ routes = { routes }
60+ resourceData = { resourceData }
61+ resourceContext = { resourceContext }
6562 >
6663 { children }
6764 </ Router >
0 commit comments