Skip to content

Commit 5563672

Browse files
authored
Fix useFormAction inheriting ?index param from child route action submission (#11025)
1 parent 39854ce commit 5563672

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

.changeset/form-action-index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": patch
3+
---
4+
5+
Fix `useFormAction` which was incorrectly inheriting the `?index` query param from child route `action` submissions

packages/react-router-dom/__tests__/data-browser-router-test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,28 @@ function testDomRouter(
26982698
"/foo"
26992699
);
27002700
});
2701+
2702+
it("does not include the index parameter if we've submitted to a child index route", async () => {
2703+
let router = createTestRouter(
2704+
createRoutesFromElements(
2705+
<Route path="/">
2706+
<Route path="foo">
2707+
<Route path="bar" element={<NoActionComponent />}>
2708+
<Route index={true} element={<h1>Index</h1>} />
2709+
</Route>
2710+
</Route>
2711+
</Route>
2712+
),
2713+
{
2714+
window: getWindow("/foo/bar?index&a=1"),
2715+
}
2716+
);
2717+
let { container } = render(<RouterProvider router={router} />);
2718+
2719+
expect(container.querySelector("form")?.getAttribute("action")).toBe(
2720+
"/foo/bar?a=1"
2721+
);
2722+
});
27012723
});
27022724

27032725
describe("index routes", () => {

packages/react-router-dom/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,11 +1551,11 @@ export function useFormAction(
15511551
// would have called useResolvedPath(".") which will never include a search
15521552
path.search = location.search;
15531553

1554-
// When grabbing search params from the URL, remove the automatically
1555-
// inserted ?index param so we match the useResolvedPath search behavior
1556-
// which would not include ?index
1557-
if (match.route.index) {
1558-
let params = new URLSearchParams(path.search);
1554+
// When grabbing search params from the URL, remove any included ?index param
1555+
// since it might not apply to our contextual route. We add it back based
1556+
// on match.route.index below
1557+
let params = new URLSearchParams(path.search);
1558+
if (params.has("index") && params.get("index") === "") {
15591559
params.delete("index");
15601560
path.search = params.toString() ? `?${params.toString()}` : "";
15611561
}

0 commit comments

Comments
 (0)