@@ -8,7 +8,7 @@ import type {
8
8
AgnosticDataRouteMatch ,
9
9
AgnosticDataRouteObject ,
10
10
ErrorResponse ,
11
- Fetcher as FetcherRR ,
11
+ Navigation ,
12
12
} from "@remix-run/router" ;
13
13
import type {
14
14
LinkProps ,
@@ -957,7 +957,13 @@ export function useActionData<T = AppData>(): SerializeFrom<T> | undefined {
957
957
*/
958
958
export function useTransition ( ) : Transition {
959
959
let navigation = useNavigation ( ) ;
960
+ return React . useMemo (
961
+ ( ) => convertNavigationToTransition ( navigation ) ,
962
+ [ navigation ]
963
+ ) ;
964
+ }
960
965
966
+ function convertNavigationToTransition ( navigation : Navigation ) : Transition {
961
967
let { location, state, formMethod, formAction, formEncType, formData } =
962
968
navigation ;
963
969
@@ -1020,13 +1026,27 @@ export function useTransition(): Transition {
1020
1026
} ;
1021
1027
return transition ;
1022
1028
} else {
1029
+ // The new router fixes a bug in useTransition where the submission
1030
+ // "action" represents the request URL not the state of the <form> in
1031
+ // the DOM. Back-port it here to maintain behavior, but useNavigation
1032
+ // will fix this bug.
1033
+ let url = new URL ( formAction , window . location . origin ) ;
1034
+
1035
+ // This typing override should be safe since this is only running for
1036
+ // GET submissions and over in @remix -run/router we have an invariant
1037
+ // if you have any non-string values in your FormData when we attempt
1038
+ // to convert them to URLSearchParams
1039
+ url . search = new URLSearchParams (
1040
+ formData . entries ( ) as unknown as [ string , string ] [ ]
1041
+ ) . toString ( ) ;
1042
+
1023
1043
// Actively "submitting" to a loader
1024
1044
let transition : TransitionStates [ "SubmittingLoader" ] = {
1025
1045
location,
1026
1046
state : "submitting" ,
1027
1047
submission : {
1028
1048
method : formMethod . toUpperCase ( ) as LoaderSubmission [ "method" ] ,
1029
- action : formAction ,
1049
+ action : url . pathname + url . search ,
1030
1050
encType : formEncType ,
1031
1051
formData : formData ,
1032
1052
key : location . key ,
@@ -1107,7 +1127,17 @@ export function useTransition(): Transition {
1107
1127
*/
1108
1128
export function useFetchers ( ) : Fetcher [ ] {
1109
1129
let fetchers = useFetchersRR ( ) ;
1110
- return fetchers . map ( convertRRFetcherToRemixFetcher ) ;
1130
+ return fetchers . map ( ( f ) =>
1131
+ convertRouterFetcherToRemixFetcher ( {
1132
+ state : f . state ,
1133
+ data : f . data ,
1134
+ formMethod : f . formMethod ,
1135
+ formAction : f . formAction ,
1136
+ formData : f . formData ,
1137
+ formEncType : f . formEncType ,
1138
+ " _hasFetcherDoneAnything " : f [ " _hasFetcherDoneAnything " ] ,
1139
+ } )
1140
+ ) ;
1111
1141
}
1112
1142
1113
1143
export type FetcherWithComponents < TData > = Fetcher < TData > & {
@@ -1128,23 +1158,28 @@ export function useFetcher<TData = any>(): FetcherWithComponents<
1128
1158
SerializeFrom < TData >
1129
1159
> {
1130
1160
let fetcherRR = useFetcherRR ( ) ;
1131
- return convertRRFetcherToRemixFetcher ( fetcherRR ) ;
1161
+ let remixFetcher = convertRouterFetcherToRemixFetcher ( {
1162
+ state : fetcherRR . state ,
1163
+ data : fetcherRR . data ,
1164
+ formMethod : fetcherRR . formMethod ,
1165
+ formAction : fetcherRR . formAction ,
1166
+ formData : fetcherRR . formData ,
1167
+ formEncType : fetcherRR . formEncType ,
1168
+ " _hasFetcherDoneAnything " : fetcherRR [ " _hasFetcherDoneAnything " ] ,
1169
+ } ) ;
1170
+ return {
1171
+ ...remixFetcher ,
1172
+ load : fetcherRR . load ,
1173
+ submit : fetcherRR . submit ,
1174
+ Form : fetcherRR . Form ,
1175
+ } ;
1132
1176
}
1133
1177
1134
- function convertRRFetcherToRemixFetcher (
1135
- fetcherRR : ReturnType < typeof useFetcherRR >
1136
- ) {
1137
- let {
1138
- state,
1139
- formMethod,
1140
- formAction,
1141
- formEncType,
1142
- formData,
1143
- data,
1144
- Form,
1145
- submit,
1146
- load,
1147
- } = fetcherRR ;
1178
+ function convertRouterFetcherToRemixFetcher (
1179
+ fetcherRR : Omit < ReturnType < typeof useFetcherRR > , "load" | "submit" | "Form" >
1180
+ ) : Fetcher {
1181
+ let { state, formMethod, formAction, formEncType, formData, data } =
1182
+ fetcherRR ;
1148
1183
1149
1184
let isActionSubmission =
1150
1185
formMethod != null &&
@@ -1158,20 +1193,10 @@ function convertRRFetcherToRemixFetcher(
1158
1193
submission : undefined ,
1159
1194
data,
1160
1195
} ;
1161
- return {
1162
- Form,
1163
- submit,
1164
- load,
1165
- ...fetcher ,
1166
- } ;
1196
+ return fetcher ;
1167
1197
} else {
1168
1198
let fetcher : FetcherStates [ "Idle" ] = IDLE_FETCHER ;
1169
- return {
1170
- Form,
1171
- submit,
1172
- load,
1173
- ...fetcher ,
1174
- } ;
1199
+ return fetcher ;
1175
1200
}
1176
1201
}
1177
1202
@@ -1205,12 +1230,7 @@ function convertRRFetcherToRemixFetcher(
1205
1230
} ,
1206
1231
data : undefined ,
1207
1232
} ;
1208
- return {
1209
- Form,
1210
- submit,
1211
- load,
1212
- ...fetcher ,
1213
- } ;
1233
+ return fetcher ;
1214
1234
} else {
1215
1235
invariant ( false , "nope" ) ;
1216
1236
}
@@ -1242,12 +1262,7 @@ function convertRRFetcherToRemixFetcher(
1242
1262
} ,
1243
1263
data,
1244
1264
} ;
1245
- return {
1246
- Form,
1247
- submit,
1248
- load,
1249
- ...fetcher ,
1250
- } ;
1265
+ return fetcher ;
1251
1266
} else {
1252
1267
let fetcher : FetcherStates [ "LoadingActionRedirect" ] = {
1253
1268
state,
@@ -1262,12 +1277,7 @@ function convertRRFetcherToRemixFetcher(
1262
1277
} ,
1263
1278
data : undefined ,
1264
1279
} ;
1265
- return {
1266
- Form,
1267
- submit,
1268
- load,
1269
- ...fetcher ,
1270
- } ;
1280
+ return fetcher ;
1271
1281
}
1272
1282
} else {
1273
1283
// The new router fixes a bug in useTransition where the submission
@@ -1306,12 +1316,7 @@ function convertRRFetcherToRemixFetcher(
1306
1316
} ,
1307
1317
data : undefined ,
1308
1318
} ;
1309
- return {
1310
- Form,
1311
- submit,
1312
- load,
1313
- ...fetcher ,
1314
- } ;
1319
+ return fetcher ;
1315
1320
}
1316
1321
}
1317
1322
}
@@ -1323,12 +1328,7 @@ function convertRRFetcherToRemixFetcher(
1323
1328
submission : undefined ,
1324
1329
data : undefined ,
1325
1330
} ;
1326
- return {
1327
- Form,
1328
- submit,
1329
- load,
1330
- ...fetcher ,
1331
- } ;
1331
+ return fetcher ;
1332
1332
}
1333
1333
1334
1334
// Dead Code Elimination magic for production builds.
0 commit comments