File tree Expand file tree Collapse file tree 1 file changed +17
-6
lines changed
src/controllers/memory-router Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react' ;
1
+ import React , { useRef } from 'react' ;
2
2
3
3
import { createMemoryHistory , MemoryHistoryBuildOptions } from 'history' ;
4
4
@@ -40,18 +40,29 @@ const getRouterProps = (memoryRouterProps: MemoryRouterProps) => {
40
40
*/
41
41
export const MemoryRouter = ( props : MemoryRouterProps ) => {
42
42
const { location, children } = props ;
43
- const config : MemoryHistoryBuildOptions = { } ;
44
43
45
- if ( location ) {
46
- config . initialEntries = [ location ] ;
44
+ const newGetHistory = ( ) =>
45
+ createMemoryHistory ( {
46
+ initialEntries : location !== undefined ? [ location ] : undefined ,
47
+ } ) ;
48
+
49
+ const historyState = useRef ( {
50
+ getHistory : newGetHistory ,
51
+ location,
52
+ } ) ;
53
+
54
+ if ( historyState . current . location !== location ) {
55
+ historyState . current . getHistory = newGetHistory ;
47
56
}
48
57
49
- const history = createMemoryHistory ( config ) ;
50
58
const routerProps = getRouterProps ( props ) ;
51
59
52
60
return (
53
61
// @ts -ignore suppress history will be overwritten warning
54
- < Router history = { history } { ...( routerProps as RouterProps ) } >
62
+ < Router
63
+ history = { historyState . current . getHistory ( ) }
64
+ { ...( routerProps as RouterProps ) }
65
+ >
55
66
{ children }
56
67
</ Router >
57
68
) ;
You can’t perform that action at this time.
0 commit comments