@@ -322,9 +322,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
322
322
loading : auto
323
323
? isPending ( resolvedKey ) ||
324
324
( revalidateOnMount
325
- ? suspense
326
- ? isPending ( resolvedKey )
327
- : true
325
+ ? previousConfig [ resolvedKey ] !== serialize ( optionsConfig )
328
326
: previousConfig [ resolvedKey ] !== serialize ( optionsConfig ) )
329
327
: false ,
330
328
error : ( hasErrors [ resolvedDataKey ] || false ) as boolean ,
@@ -337,7 +335,11 @@ export function useFetch<FetchDataType = any, BodyType = any>(
337
335
loading : false ,
338
336
error : false ,
339
337
completedAttempts : false
340
- } )
338
+ } ) . current
339
+
340
+ const inDeps = ( k : keyof typeof thisDeps ) => {
341
+ return thisDeps [ k ]
342
+ }
341
343
342
344
const { data, loading, online, error, completedAttempts } = fetchState
343
345
@@ -833,32 +835,10 @@ export function useFetch<FetchDataType = any, BodyType = any>(
833
835
}
834
836
}
835
837
} finally {
836
- setFetchState ( p => {
837
- const n = {
838
- ...p ,
839
- data : thisDeps . current . data ? $$data ?? p . data : p . data ,
840
- online : thisDeps . current . online ? p . online : p . online ,
841
- loading : thisDeps . current . loading
842
- ? rpc ?. loading ?? false
843
- : p . loading ,
844
- error : thisDeps . current . error
845
- ? isDefined ( $$error )
846
- ? $$error
847
- : p . error
848
- : p . error ,
849
- completedAttempts : thisDeps . current . completedAttempts
850
- ? $$completedAttempts ?? p . completedAttempts
851
- : p . completedAttempts
852
- }
853
- if ( jsonCompare ( n , p ) ) return p
854
- return n
855
- } )
856
-
857
838
runningRequests [ resolvedKey ] = false
858
839
suspenseInitialized [ resolvedKey ] = true
859
840
860
841
requestsProvider . emit ( resolvedKey , {
861
- requestCallId,
862
842
error :
863
843
hasErrors [ resolvedKey ] || hasErrors [ resolvedDataKey ] || false ,
864
844
...rpc ,
@@ -875,7 +855,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
875
855
}
876
856
} ,
877
857
[
878
- thisDeps . current ,
858
+ thisDeps ,
879
859
canRevalidate ,
880
860
ctx . auto ,
881
861
stringDeps ,
@@ -960,28 +940,21 @@ export function useFetch<FetchDataType = any, BodyType = any>(
960
940
if ( v . requestCallId !== requestCallId ) {
961
941
if ( ! willSuspend [ resolvedKey ] ) {
962
942
queue ( ( ) => {
963
- setFetchState ( p => {
964
- const n = {
965
- ...p ,
966
- data : thisDeps . current . data
967
- ? ! jsonCompare ( $data , p . data )
968
- ? $data
969
- : p . data ?? p . data
970
- : p . data ,
971
- online : thisDeps . current . online ? online ?? p . online : p . online ,
972
- loading : thisDeps . current . loading
973
- ? loading ?? p . loading
974
- : p . loading ,
975
- error : thisDeps . current . error ? Boolean ( $error ) : p . error ,
976
- completedAttempts : thisDeps . current . completedAttempts
977
- ? completedAttempts ?? p . completedAttempts
978
- : p . completedAttempts
979
- }
980
-
981
- if ( jsonCompare ( n , p ) ) return p
982
-
983
- return n
984
- } )
943
+ if ( inDeps ( 'data' ) ) {
944
+ setData ( $data )
945
+ }
946
+ if ( inDeps ( 'online' ) ) {
947
+ setOnline ( online )
948
+ }
949
+ if ( inDeps ( 'loading' ) ) {
950
+ setLoading ( loading )
951
+ }
952
+ if ( inDeps ( 'error' ) ) {
953
+ setError ( $error )
954
+ }
955
+ if ( inDeps ( 'completedAttempts' ) ) {
956
+ setCompletedAttempts ( completedAttempts )
957
+ }
985
958
} )
986
959
}
987
960
}
@@ -993,7 +966,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
993
966
requestsProvider . removeListener ( resolvedKey , waitFormUpdates )
994
967
}
995
968
} , [
996
- thisDeps . current ,
969
+ thisDeps ,
997
970
JSON . stringify ( optionsConfig ) ,
998
971
resolvedKey ,
999
972
resolvedDataKey ,
@@ -1172,7 +1145,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1172
1145
online : false ,
1173
1146
error : true
1174
1147
} )
1175
- if ( thisDeps . current . online ) setOnline ( false )
1148
+ if ( inDeps ( ' online' ) ) setOnline ( false )
1176
1149
}
1177
1150
}
1178
1151
} , getMiliseconds ( attemptInterval as TimeSpan ) )
@@ -1225,22 +1198,25 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1225
1198
}
1226
1199
return d
1227
1200
} ,
1228
- [ serialize ( serialize ( optionsConfig ) ) , fetchState ]
1201
+ [ serialize ( serialize ( optionsConfig ) ) , fetchState , thisDeps ]
1229
1202
)
1230
1203
1231
1204
if ( ! suspense ) {
1232
1205
if ( url !== '' ) {
1233
1206
suspenseInitialized [ resolvedKey ] = true
1234
1207
}
1235
1208
}
1236
- useEffect ( ( ) => {
1209
+
1210
+ React . useLayoutEffect ( ( ) => {
1237
1211
if ( url !== '' ) {
1238
1212
if ( ! jsonCompare ( previousProps [ resolvedKey ] , optionsConfig ) ) {
1239
1213
abortControllers [ resolvedKey ] ?. abort ( )
1240
- queue ( initializeRevalidation )
1214
+ if ( inDeps ( 'data' ) ) {
1215
+ queue ( initializeRevalidation )
1216
+ }
1241
1217
}
1242
1218
}
1243
- } , [ serialize ( optionsConfig ) ] )
1219
+ } , [ serialize ( optionsConfig ) , thisDeps ] )
1244
1220
1245
1221
if ( suspense ) {
1246
1222
if ( auto ) {
@@ -1260,30 +1236,34 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1260
1236
}
1261
1237
}
1262
1238
1263
- React . useMemo ( ( ) => {
1239
+ React . useLayoutEffect ( ( ) => {
1264
1240
if ( ! runningRequests [ resolvedKey ] && isExpired ) {
1265
1241
if ( windowExists ) {
1266
1242
if ( canRevalidate && url !== '' ) {
1267
1243
if ( ! jsonCompare ( previousConfig [ resolvedKey ] , optionsConfig ) ) {
1268
1244
if ( ! isPending ( resolvedKey ) ) {
1269
- initializeRevalidation ( )
1245
+ if ( inDeps ( 'data' ) ) {
1246
+ initializeRevalidation ( )
1247
+ }
1270
1248
} else {
1271
1249
setLoading ( true )
1272
1250
}
1273
1251
}
1274
1252
}
1275
1253
}
1276
1254
}
1277
- } , [ resolvedKey , serialize ( optionsConfig ) , canRevalidate ] )
1255
+ } , [ resolvedKey , serialize ( optionsConfig ) , canRevalidate , thisDeps ] )
1278
1256
1279
- useEffect ( ( ) => {
1257
+ React . useLayoutEffect ( ( ) => {
1280
1258
const revalidateAfterUnmount = revalidateOnMount
1281
1259
? true
1282
1260
: previousConfig [ resolvedKey ] !== serialize ( optionsConfig )
1283
1261
1284
1262
function revalidate ( ) {
1285
1263
if ( ! debounce && ! canDebounce [ resolvedKey ] ) {
1286
- initializeRevalidation ( )
1264
+ if ( inDeps ( 'data' ) ) {
1265
+ initializeRevalidation ( )
1266
+ }
1287
1267
}
1288
1268
}
1289
1269
@@ -1298,7 +1278,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1298
1278
}
1299
1279
1300
1280
// eslint-disable-next-line react-hooks/exhaustive-deps
1301
- } , [ serialize ( optionsConfig ) ] )
1281
+ } , [ serialize ( optionsConfig ) , thisDeps ] )
1302
1282
1303
1283
useEffect ( ( ) => {
1304
1284
function addFocusListener ( ) {
@@ -1411,7 +1391,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1411
1391
? new Date ( cacheProvider . get ( 'expiration' + resolvedDataKey ) )
1412
1392
: null
1413
1393
1414
- const isFailed = hasErrors [ resolvedDataKey ] || error
1394
+ const isFailed = hasErrors [ resolvedDataKey ] || hasErrors [ resolvedKey ] || error
1415
1395
1416
1396
const responseData =
1417
1397
( error && isFailed ? ( cacheIfError ? thisCache : null ) : thisCache ) ?? def
@@ -1426,64 +1406,64 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1426
1406
1427
1407
return {
1428
1408
get revalidating ( ) {
1429
- thisDeps . current . loading = true
1409
+ thisDeps . loading = true
1430
1410
return oneRequestResolved && isLoading
1431
1411
} ,
1432
1412
get hasData ( ) {
1433
- thisDeps . current . data = true
1413
+ thisDeps . data = true
1434
1414
return oneRequestResolved
1435
1415
} ,
1436
1416
get success ( ) {
1437
- thisDeps . current . loading = true
1438
- thisDeps . current . error = true
1417
+ thisDeps . loading = true
1418
+ thisDeps . error = true
1439
1419
return isSuccess
1440
1420
} ,
1441
1421
get loadingFirst ( ) {
1442
- thisDeps . current . loading = true
1422
+ thisDeps . loading = true
1443
1423
return loadingFirst
1444
1424
} ,
1445
1425
get requestStart ( ) {
1446
- thisDeps . current . loading = true
1426
+ thisDeps . loading = true
1447
1427
return getDateIfValid ( $requestStart )
1448
1428
} ,
1449
1429
get requestEnd ( ) {
1450
- thisDeps . current . loading = true
1430
+ thisDeps . loading = true
1451
1431
return getDateIfValid ( $requestEnd )
1452
1432
} ,
1453
1433
get expiration ( ) {
1454
- thisDeps . current . loading = true
1434
+ thisDeps . loading = true
1455
1435
return getDateIfValid ( isFailed ? null : expirationDate )
1456
1436
} ,
1457
1437
get responseTime ( ) {
1458
- thisDeps . current . loading = true
1438
+ thisDeps . loading = true
1459
1439
return requestResponseTimes [ resolvedDataKey ] ?? null
1460
1440
} ,
1461
1441
get data ( ) {
1462
- thisDeps . current . data = true
1442
+ thisDeps . data = true
1463
1443
return responseData
1464
1444
} ,
1465
1445
get loading ( ) {
1466
- thisDeps . current . loading = true
1446
+ thisDeps . loading = true
1467
1447
return isLoading
1468
1448
} ,
1469
1449
get error ( ) {
1470
- thisDeps . current . error = true
1450
+ thisDeps . error = true
1471
1451
return isFailed || false
1472
1452
} ,
1473
1453
get online ( ) {
1474
- thisDeps . current . online = true
1454
+ thisDeps . online = true
1475
1455
return online
1476
1456
} ,
1477
1457
get code ( ) {
1478
- thisDeps . current . loading = true
1458
+ thisDeps . loading = true
1479
1459
return statusCodes [ resolvedKey ]
1480
1460
} ,
1481
1461
get reFetch ( ) {
1482
- thisDeps . current . loading = true
1462
+ thisDeps . loading = true
1483
1463
return reValidate
1484
1464
} ,
1485
1465
get mutate ( ) {
1486
- thisDeps . current . data = true
1466
+ thisDeps . data = true
1487
1467
return forceMutate
1488
1468
} ,
1489
1469
get fetcher ( ) {
@@ -1508,7 +1488,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1508
1488
} ,
1509
1489
config : __config ,
1510
1490
get response ( ) {
1511
- thisDeps . current . loading = true
1491
+ thisDeps . loading = true
1512
1492
return lastResponses [ resolvedKey ]
1513
1493
} ,
1514
1494
id,
0 commit comments