Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve state from initialEntries #9288

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed this was leftover from the DataMemoryRouter -> RouterProvider refactor

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