File tree Expand file tree Collapse file tree 3 files changed +32
-5
lines changed
packages/react-router-dom Expand file tree Collapse file tree 3 files changed +32
-5
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " react-router-dom " : patch
3
+ ---
4
+
5
+ Fix ` useFormAction ` which was incorrectly inheriting the ` ?index ` query param from child route ` action ` submissions
Original file line number Diff line number Diff line change @@ -2698,6 +2698,28 @@ function testDomRouter(
2698
2698
"/foo"
2699
2699
) ;
2700
2700
} ) ;
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
+ } ) ;
2701
2723
} ) ;
2702
2724
2703
2725
describe ( "index routes" , ( ) => {
Original file line number Diff line number Diff line change @@ -1551,11 +1551,11 @@ export function useFormAction(
1551
1551
// would have called useResolvedPath(".") which will never include a search
1552
1552
path . search = location . search ;
1553
1553
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" ) === "" ) {
1559
1559
params . delete ( "index" ) ;
1560
1560
path . search = params . toString ( ) ? `?${ params . toString ( ) } ` : "" ;
1561
1561
}
You can’t perform that action at this time.
0 commit comments