Skip to content

Commit be20c31

Browse files
committed
differentiate between no action and '.'
1 parent 0be6f72 commit be20c31

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

packages/remix-react/components.tsx

+13-23
Original file line numberDiff line numberDiff line change
@@ -994,36 +994,26 @@ export function useFormAction(
994994
method: FormMethod = "get"
995995
): string {
996996
let { id } = useRemixRouteContext();
997+
let resolvedPath = useResolvedPath(action ?? ".");
997998

998-
// The documented behavior of a form without an explicit action prop is:
999-
// - Forms without an action prop (<Form method="post">) will automatically
1000-
// post to the same route within which they are rendered.
1001-
//
1002-
// The problem with setting the default action to `.` is that
1003-
// `useResolvedPath(".")` excludes search params. This is the intended
1004-
// behavior of useResolvedPath, but it's unexpected for forms without an
1005-
// action prop since the search params are an important part of the current
1006-
// location, and the action function should receive those in the request URL.
1007-
//
1008-
// If the action prop is omitted, we'll append the current route's search
1009-
// string to the current location if it exists. If the action is explicitly
1010-
// set to `.` it will resolve without the search params to match the intended
1011-
// behavior of useResolvedPath, resolving the same URL as `<Link to="." />`
999+
// Previously we set the default action to ".". The problem with this is that
1000+
// `useResolvedPath(".")` excludes search params and the hash of the resolved
1001+
// URL. This is the intended behavior of when "." is specifically provided as
1002+
// the form action, but inconsistent w/ browsers when the action is omitted.
10121003
// https://github.com/remix-run/remix/issues/927
1013-
let { search: currentSearch } = useLocation();
1014-
let actionIsCurrentRoute = action === undefined || action === ".";
1015-
let path = useResolvedPath(
1016-
action === undefined ? { pathname: ".", search: currentSearch } : action
1017-
);
1004+
let location = useLocation();
1005+
let { search, hash } = resolvedPath;
1006+
if (action === undefined) {
1007+
search = location.search;
1008+
hash = location.hash;
1009+
}
10181010

1019-
let search = path.search;
10201011
let isIndexRoute = id.endsWith("/index");
1021-
1022-
if (actionIsCurrentRoute && isIndexRoute) {
1012+
if ((action === undefined || action === ".") && isIndexRoute) {
10231013
search = search ? search.replace(/^\?/, "?index&") : "?index";
10241014
}
10251015

1026-
return path.pathname + search;
1016+
return resolvedPath.pathname + search + hash;
10271017
}
10281018

10291019
export interface SubmitOptions {

0 commit comments

Comments
 (0)