Skip to content

Commit 4a2f26d

Browse files
committed
Simplified prop destruction to avoid type casts
1 parent 9ccaa05 commit 4a2f26d

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

src/controllers/memory-router/index.tsx

+46-41
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,72 @@
11
import React, { useRef } from 'react';
22

3-
import { createMemoryHistory, MemoryHistoryBuildOptions } from 'history';
3+
import { createMemoryHistory } from 'history';
44

55
import { Router } from '../router';
6-
import { RouterProps } from '../router/types';
76

87
import { 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-
};
9+
const lazy = <T extends any>(callback: () => T) => {
10+
let firstCall = true;
11+
let current: T | undefined = undefined;
2512

26-
if (resourceData) {
27-
routerProps = { ...routerProps, resourceData };
28-
}
13+
return () => {
14+
if (firstCall) {
15+
current = callback();
16+
firstCall = false;
17+
}
2918

30-
if (resourceContext) {
31-
routerProps = { ...routerProps, resourceContext };
32-
}
33-
34-
return routerProps;
19+
return current;
20+
};
3521
};
3622

37-
/**
38-
* Ensures the router store uses memory history.
39-
*
40-
*/
41-
export const MemoryRouter = (props: MemoryRouterProps) => {
42-
const { location, children } = props;
43-
44-
const newGetHistory = () =>
23+
const useMemoryHistory = (location: string | undefined) => {
24+
const newGetHistory = lazy(() =>
4525
createMemoryHistory({
4626
initialEntries: location !== undefined ? [location] : undefined,
47-
});
27+
})
28+
);
4829

49-
const historyState = useRef({
30+
const historyStateCandidate = {
5031
getHistory: newGetHistory,
5132
location,
52-
});
33+
};
34+
35+
const historyState = useRef(historyStateCandidate);
5336

54-
if (historyState.current.location !== location) {
55-
historyState.current.getHistory = newGetHistory;
37+
if (historyState.current.location !== historyStateCandidate.location) {
38+
historyState.current = historyStateCandidate;
5639
}
5740

58-
const routerProps = getRouterProps(props);
41+
return historyState.current.getHistory();
42+
};
43+
44+
/**
45+
* Ensures the router store uses memory history.
46+
*
47+
*/
48+
export const MemoryRouter = ({
49+
isStatic = false,
50+
isGlobal = true,
51+
location,
52+
children,
53+
basePath,
54+
routes,
55+
resourceData,
56+
resourceContext,
57+
}: MemoryRouterProps) => {
58+
const history = useMemoryHistory(location);
5959

6060
return (
6161
// @ts-ignore suppress history will be overwritten warning
6262
<Router
63-
history={historyState.current.getHistory()}
64-
{...(routerProps as RouterProps)}
63+
isStatic={isStatic}
64+
isGlobal={isGlobal}
65+
history={history}
66+
basePath={basePath}
67+
routes={routes}
68+
resourceData={resourceData}
69+
resourceContext={resourceContext}
6570
>
6671
{children}
6772
</Router>

0 commit comments

Comments
 (0)