Skip to content

Commit 9ccaa05

Browse files
committed
No longer recreating histories within MemoryRouter
1 parent 51c18ff commit 9ccaa05

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/controllers/memory-router/index.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useRef } from 'react';
22

33
import { createMemoryHistory, MemoryHistoryBuildOptions } from 'history';
44

@@ -40,18 +40,29 @@ const getRouterProps = (memoryRouterProps: MemoryRouterProps) => {
4040
*/
4141
export const MemoryRouter = (props: MemoryRouterProps) => {
4242
const { location, children } = props;
43-
const config: MemoryHistoryBuildOptions = {};
4443

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;
4756
}
4857

49-
const history = createMemoryHistory(config);
5058
const routerProps = getRouterProps(props);
5159

5260
return (
5361
// @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+
>
5566
{children}
5667
</Router>
5768
);

0 commit comments

Comments
 (0)