diff --git a/.changeset/heavy-waves-pump.md b/.changeset/heavy-waves-pump.md
new file mode 100644
index 0000000000..4d4b8593b4
--- /dev/null
+++ b/.changeset/heavy-waves-pump.md
@@ -0,0 +1,6 @@
+---
+"react-router": patch
+"@remix-run/router": patch
+---
+
+fix: preserve state from initialEntries (#9288)
diff --git a/packages/react-router-dom/index.tsx b/packages/react-router-dom/index.tsx
index 7f699dd36b..6941de6fe6 100644
--- a/packages/react-router-dom/index.tsx
+++ b/packages/react-router-dom/index.tsx
@@ -74,7 +74,6 @@ export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
- DataMemoryRouterProps,
DataRouteMatch,
DataRouteObject,
Fetcher,
diff --git a/packages/react-router-native/index.tsx b/packages/react-router-native/index.tsx
index b386879549..9bd819a5c3 100644
--- a/packages/react-router-native/index.tsx
+++ b/packages/react-router-native/index.tsx
@@ -23,7 +23,6 @@ export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
- DataMemoryRouterProps,
DataRouteMatch,
DataRouteObject,
Fetcher,
diff --git a/packages/react-router/__tests__/useLocation-test.tsx b/packages/react-router/__tests__/useLocation-test.tsx
index 2c7cfa6c69..121073a0d5 100644
--- a/packages/react-router/__tests__/useLocation-test.tsx
+++ b/packages/react-router/__tests__/useLocation-test.tsx
@@ -61,4 +61,27 @@ describe("useLocation", () => {
`);
});
+
+ it("preserves state from initialEntries", () => {
+ let renderer: TestRenderer.ReactTestRenderer;
+ TestRenderer.act(() => {
+ renderer = TestRenderer.create(
+
+ {"pathname":"/example","search":"","hash":"","state":{"my":"state"},"key":"my-key"} ++ `); + }); }); diff --git a/packages/react-router/index.ts b/packages/react-router/index.ts index d2c28e39d1..64cbe78ebe 100644 --- a/packages/react-router/index.ts +++ b/packages/react-router/index.ts @@ -36,7 +36,6 @@ import { } from "@remix-run/router"; import type { - DataMemoryRouterProps, AwaitProps, MemoryRouterProps, NavigateProps, @@ -113,7 +112,6 @@ export type { ActionFunction, ActionFunctionArgs, AwaitProps, - DataMemoryRouterProps, DataRouteMatch, DataRouteObject, Fetcher, diff --git a/packages/react-router/lib/components.tsx b/packages/react-router/lib/components.tsx index 30df209dac..cd1795e95a 100644 --- a/packages/react-router/lib/components.tsx +++ b/packages/react-router/lib/components.tsx @@ -109,16 +109,6 @@ export function RouterProvider({ ); } -export interface DataMemoryRouterProps { - basename?: string; - children?: React.ReactNode; - initialEntries?: InitialEntry[]; - initialIndex?: number; - hydrationData?: HydrationState; - fallbackElement?: React.ReactNode; - routes?: RouteObject[]; -} - export interface MemoryRouterProps { basename?: string; children?: React.ReactNode; diff --git a/packages/router/__tests__/memory-test.ts b/packages/router/__tests__/memory-test.ts index d724199a95..e5233b50e3 100644 --- a/packages/router/__tests__/memory-test.ts +++ b/packages/router/__tests__/memory-test.ts @@ -173,4 +173,32 @@ describe("a memory history with some initial entries", () => { key: expect.any(String), }); }); + + it("allows initial entries to have state and keys", () => { + let history = createMemoryHistory({ + initialEntries: [ + { pathname: "/one", state: "1", key: "1" }, + { pathname: "/two", state: "2", key: "2" }, + ], + }); + + expect(history.index).toBe(1); + expect(history.location).toMatchObject({ + pathname: "/two", + search: "", + hash: "", + state: "2", + key: "2", + }); + + history.go(-1); + expect(history.index).toBe(0); + expect(history.location).toMatchObject({ + pathname: "/one", + search: "", + hash: "", + state: "1", + key: "1", + }); + }); }); diff --git a/packages/router/history.ts b/packages/router/history.ts index 7a50dcdb41..ce6d0c1fca 100644 --- a/packages/router/history.ts +++ b/packages/router/history.ts @@ -208,7 +208,11 @@ export function createMemoryHistory( let { initialEntries = ["/"], initialIndex, v5Compat = false } = options; let entries: Location[]; // Declare so we can access from createMemoryLocation entries = initialEntries.map((entry, index) => - createMemoryLocation(entry, null, index === 0 ? "default" : undefined) + createMemoryLocation( + entry, + typeof entry === "string" ? null : entry.state, + index === 0 ? "default" : undefined + ) ); let index = clampIndex( initialIndex == null ? entries.length - 1 : initialIndex