diff --git a/src/controllers/memory-router/index.tsx b/src/controllers/memory-router/index.tsx index e33f2909..cf630a05 100644 --- a/src/controllers/memory-router/index.tsx +++ b/src/controllers/memory-router/index.tsx @@ -1,57 +1,73 @@ -import React from 'react'; +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; + + return () => { + if (firstCall) { + current = callback(); + firstCall = false; + } + + return current; }; +}; - if (resourceData) { - routerProps = { ...routerProps, resourceData }; - } +const useMemoryHistory = (location: string | undefined) => { + const newGetHistory = lazy(() => + createMemoryHistory({ + initialEntries: location !== undefined ? [location] : undefined, + }) + ); - if (resourceContext) { - routerProps = { ...routerProps, resourceContext }; + const historyStateCandidate = { + getHistory: newGetHistory, + location, + }; + + const historyState = useRef(historyStateCandidate); + + if (historyState.current.location !== historyStateCandidate.location) { + historyState.current = historyStateCandidate; } - return routerProps; + return historyState.current.getHistory(); }; /** * Ensures the router store uses memory history. * */ -export const MemoryRouter = (props: MemoryRouterProps) => { - const { location, children } = props; - const config: MemoryHistoryBuildOptions = {}; - - if (location) { - config.initialEntries = [location]; - } - - const history = createMemoryHistory(config); - const routerProps = getRouterProps(props); +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} );