From 9ccaa05c897006197061bf84cbd1671cd3918963 Mon Sep 17 00:00:00 2001 From: Patrick Shaw Date: Wed, 4 May 2022 14:32:40 +1000 Subject: [PATCH 1/2] No longer recreating histories within MemoryRouter --- src/controllers/memory-router/index.tsx | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/controllers/memory-router/index.tsx b/src/controllers/memory-router/index.tsx index e33f2909..3b4a41eb 100644 --- a/src/controllers/memory-router/index.tsx +++ b/src/controllers/memory-router/index.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useRef } from 'react'; import { createMemoryHistory, MemoryHistoryBuildOptions } from 'history'; @@ -40,18 +40,29 @@ const getRouterProps = (memoryRouterProps: MemoryRouterProps) => { */ export const MemoryRouter = (props: MemoryRouterProps) => { const { location, children } = props; - const config: MemoryHistoryBuildOptions = {}; - if (location) { - config.initialEntries = [location]; + const newGetHistory = () => + createMemoryHistory({ + initialEntries: location !== undefined ? [location] : undefined, + }); + + const historyState = useRef({ + getHistory: newGetHistory, + location, + }); + + if (historyState.current.location !== location) { + historyState.current.getHistory = newGetHistory; } - const history = createMemoryHistory(config); const routerProps = getRouterProps(props); return ( // @ts-ignore suppress history will be overwritten warning - + {children} ); From 4a2f26ddaf308f0584e6210e379d50be46087c04 Mon Sep 17 00:00:00 2001 From: Patrick Shaw Date: Wed, 4 May 2022 14:34:50 +1000 Subject: [PATCH 2/2] Simplified prop destruction to avoid type casts --- src/controllers/memory-router/index.tsx | 87 +++++++++++++------------ 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/src/controllers/memory-router/index.tsx b/src/controllers/memory-router/index.tsx index 3b4a41eb..cf630a05 100644 --- a/src/controllers/memory-router/index.tsx +++ b/src/controllers/memory-router/index.tsx @@ -1,67 +1,72 @@ import React, { useRef } from 'react'; -import { createMemoryHistory, MemoryHistoryBuildOptions } from 'history'; +import { createMemoryHistory } from 'history'; import { Router } from '../router'; -import { RouterProps } from '../router/types'; import { MemoryRouterProps } from '../../common/types'; -const getRouterProps = (memoryRouterProps: MemoryRouterProps) => { - const { - isStatic = false, - isGlobal = true, - basePath, - routes, - resourceData, - resourceContext, - } = memoryRouterProps; - let routerProps: Partial = { - basePath, - routes, - isStatic, - isGlobal, - }; +const lazy = (callback: () => T) => { + let firstCall = true; + let current: T | undefined = undefined; - if (resourceData) { - routerProps = { ...routerProps, resourceData }; - } + return () => { + if (firstCall) { + current = callback(); + firstCall = false; + } - if (resourceContext) { - routerProps = { ...routerProps, resourceContext }; - } - - return routerProps; + return current; + }; }; -/** - * Ensures the router store uses memory history. - * - */ -export const MemoryRouter = (props: MemoryRouterProps) => { - const { location, children } = props; - - const newGetHistory = () => +const useMemoryHistory = (location: string | undefined) => { + const newGetHistory = lazy(() => createMemoryHistory({ initialEntries: location !== undefined ? [location] : undefined, - }); + }) + ); - const historyState = useRef({ + const historyStateCandidate = { getHistory: newGetHistory, location, - }); + }; + + const historyState = useRef(historyStateCandidate); - if (historyState.current.location !== location) { - historyState.current.getHistory = newGetHistory; + if (historyState.current.location !== historyStateCandidate.location) { + historyState.current = historyStateCandidate; } - const routerProps = getRouterProps(props); + return historyState.current.getHistory(); +}; + +/** + * Ensures the router store uses memory history. + * + */ +export const MemoryRouter = ({ + isStatic = false, + isGlobal = true, + location, + children, + basePath, + routes, + resourceData, + resourceContext, +}: MemoryRouterProps) => { + const history = useMemoryHistory(location); return ( // @ts-ignore suppress history will be overwritten warning {children}