Components

MemoryRouter

Edit this page

The MemoryRouter can be used to route while keeping the entire routing history within internal memory. Other routers keep history externally such as in the url like the <HashRouter> or in the browser history like <Router>.

Keeping the history in memory is useful when you want full control over the routing history. Since MemoryRouter can manipulate MemoryHistory, it is often used for testing purposes.

import { MemoryRouter, createMemoryHistory, A } from "@solidjs/router";
import { Suspense } from "solid-js";
export default function App() {
const history = createMemoryHistory();
const toHome = () => {
history.set({ value: "/" });
};
const toAbout = () => {
history.set({ value: "/about" });
};
return (
<>
<button onClick={toHome}>{'"/"'}</button>
<button onClick={toAbout}>{'"/about"'}</button>
<MemoryRouter
history={history}
root={(props) => <Suspense>{props.children}</Suspense>}
>
{/*... routes */}
</MemoryRouter>
</>
);
}

In this example, a history object is pre-filled to navigate to the /about route, which is then passed to the MemoryRouter.


Manipulating the history

The MemoryHistory object contains the following methods, which you can use to control the navigation of your app.

  • The get method retrieves the current route as a string, while the set method navigates to a new route, allowing for optional parameters like replacing the current history entry or scrolling to a DOM element id.
  • The back and forward methods mimic the browser's back and forward buttons, respectively, and the go method navigates a specific number of steps in the history, either backward or forward.
  • The listen method registers a callback to be called on navigation change.

Properties

MemoryHistory

MethodSignatureDescription
getget(): stringReturns the current route.
setset({ value: string, scroll?: boolean, replace?: boolean })Navigates to a new route. value: URL to navigate to. scroll: Scrolls to element by ID (default: false). replace: Replaces the current history entry (default: false).
backback(): voidNavigates 1 step back in the history. Corresponds to go(-1).
forwardforward(): voidNavigates 1 step forward in the history. Corresponds to go(1).
gogo(n: number): voidNavigates n steps in the history. Negative for back, positive for forward. Clamped to history length.
listenlisten(listener: (value: string) => void): () => voidRegisters a listener that will be called on navigation.

MemoryRouter

PropertyTypeDescription
childrenJSX.Element, RouteDefinition, or RouteDefinition[]The route definitions
rootComponentTop level layout component
basestringBase url to use for matching routes
actionBasestringRoot url for server actions, default: /_server
preloadbooleanEnables/disables preloads globally, default: true
explicitLinksbooleanDisables all anchors being intercepted and instead requires <A>. default: false
historyMemoryHistoryAn optionally pre-filled and mutable history object which will store any navigation events
Report an issue with this page