Skip to content

Commit

Permalink
fix: preserve state from initialEntries (#9288)
Browse files Browse the repository at this point in the history
* fix: preserve state from initialEntries

* chore: Remove unused/stale DataMemoryRouterProps

* chore: add changeset
  • Loading branch information
brophdawg11 committed Sep 19, 2022
1 parent e20a6f7 commit 8b00e7a
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-waves-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"react-router": patch
"@remix-run/router": patch
---

fix: preserve state from initialEntries (#9288)
1 change: 0 additions & 1 deletion packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
DataMemoryRouterProps,
DataRouteMatch,
DataRouteObject,
Fetcher,
Expand Down
1 change: 0 additions & 1 deletion packages/react-router-native/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
DataMemoryRouterProps,
DataRouteMatch,
DataRouteObject,
Fetcher,
Expand Down
23 changes: 23 additions & 0 deletions packages/react-router/__tests__/useLocation-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,27 @@ describe("useLocation", () => {
</div>
`);
});

it("preserves state from initialEntries", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter
initialEntries={[
{ pathname: "/example", state: { my: "state" }, key: "my-key" },
]}
>
<Routes>
<Route path={"/example"} element={<ShowLocation />} />
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<pre>
{"pathname":"/example","search":"","hash":"","state":{"my":"state"},"key":"my-key"}
</pre>
`);
});
});
2 changes: 0 additions & 2 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
} from "@remix-run/router";

import type {
DataMemoryRouterProps,
AwaitProps,
MemoryRouterProps,
NavigateProps,
Expand Down Expand Up @@ -113,7 +112,6 @@ export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
DataMemoryRouterProps,
DataRouteMatch,
DataRouteObject,
Fetcher,
Expand Down
10 changes: 0 additions & 10 deletions packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions packages/router/__tests__/memory-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
});
6 changes: 5 additions & 1 deletion packages/router/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8b00e7a

Please sign in to comment.