Skip to content

Commit 8b00e7a

Browse files
authored
fix: preserve state from initialEntries (#9288)
* fix: preserve state from initialEntries * chore: Remove unused/stale DataMemoryRouterProps * chore: add changeset
1 parent e20a6f7 commit 8b00e7a

File tree

8 files changed

+62
-15
lines changed

8 files changed

+62
-15
lines changed

.changeset/heavy-waves-pump.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"react-router": patch
3+
"@remix-run/router": patch
4+
---
5+
6+
fix: preserve state from initialEntries (#9288)

packages/react-router-dom/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ export type {
7474
ActionFunction,
7575
ActionFunctionArgs,
7676
AwaitProps,
77-
DataMemoryRouterProps,
7877
DataRouteMatch,
7978
DataRouteObject,
8079
Fetcher,

packages/react-router-native/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export type {
2323
ActionFunction,
2424
ActionFunctionArgs,
2525
AwaitProps,
26-
DataMemoryRouterProps,
2726
DataRouteMatch,
2827
DataRouteObject,
2928
Fetcher,

packages/react-router/__tests__/useLocation-test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,27 @@ describe("useLocation", () => {
6161
</div>
6262
`);
6363
});
64+
65+
it("preserves state from initialEntries", () => {
66+
let renderer: TestRenderer.ReactTestRenderer;
67+
TestRenderer.act(() => {
68+
renderer = TestRenderer.create(
69+
<MemoryRouter
70+
initialEntries={[
71+
{ pathname: "/example", state: { my: "state" }, key: "my-key" },
72+
]}
73+
>
74+
<Routes>
75+
<Route path={"/example"} element={<ShowLocation />} />
76+
</Routes>
77+
</MemoryRouter>
78+
);
79+
});
80+
81+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
82+
<pre>
83+
{"pathname":"/example","search":"","hash":"","state":{"my":"state"},"key":"my-key"}
84+
</pre>
85+
`);
86+
});
6487
});

packages/react-router/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from "@remix-run/router";
3737

3838
import type {
39-
DataMemoryRouterProps,
4039
AwaitProps,
4140
MemoryRouterProps,
4241
NavigateProps,
@@ -113,7 +112,6 @@ export type {
113112
ActionFunction,
114113
ActionFunctionArgs,
115114
AwaitProps,
116-
DataMemoryRouterProps,
117115
DataRouteMatch,
118116
DataRouteObject,
119117
Fetcher,

packages/react-router/lib/components.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,6 @@ export function RouterProvider({
109109
);
110110
}
111111

112-
export interface DataMemoryRouterProps {
113-
basename?: string;
114-
children?: React.ReactNode;
115-
initialEntries?: InitialEntry[];
116-
initialIndex?: number;
117-
hydrationData?: HydrationState;
118-
fallbackElement?: React.ReactNode;
119-
routes?: RouteObject[];
120-
}
121-
122112
export interface MemoryRouterProps {
123113
basename?: string;
124114
children?: React.ReactNode;

packages/router/__tests__/memory-test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,32 @@ describe("a memory history with some initial entries", () => {
173173
key: expect.any(String),
174174
});
175175
});
176+
177+
it("allows initial entries to have state and keys", () => {
178+
let history = createMemoryHistory({
179+
initialEntries: [
180+
{ pathname: "/one", state: "1", key: "1" },
181+
{ pathname: "/two", state: "2", key: "2" },
182+
],
183+
});
184+
185+
expect(history.index).toBe(1);
186+
expect(history.location).toMatchObject({
187+
pathname: "/two",
188+
search: "",
189+
hash: "",
190+
state: "2",
191+
key: "2",
192+
});
193+
194+
history.go(-1);
195+
expect(history.index).toBe(0);
196+
expect(history.location).toMatchObject({
197+
pathname: "/one",
198+
search: "",
199+
hash: "",
200+
state: "1",
201+
key: "1",
202+
});
203+
});
176204
});

packages/router/history.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ export function createMemoryHistory(
208208
let { initialEntries = ["/"], initialIndex, v5Compat = false } = options;
209209
let entries: Location[]; // Declare so we can access from createMemoryLocation
210210
entries = initialEntries.map((entry, index) =>
211-
createMemoryLocation(entry, null, index === 0 ? "default" : undefined)
211+
createMemoryLocation(
212+
entry,
213+
typeof entry === "string" ? null : entry.state,
214+
index === 0 ? "default" : undefined
215+
)
212216
);
213217
let index = clampIndex(
214218
initialIndex == null ? entries.length - 1 : initialIndex

0 commit comments

Comments
 (0)