@@ -242,11 +242,11 @@ result.refetch;
242
242
243
243
``` tsx
244
244
// 실제 예제
245
+ // 💡 queryFn의 반환 타입을 지정해주면 useQuery의 타입 추론이 원활합니다.
245
246
const getAllSuperHero = async (): Promise <AxiosResponse <Hero []>> => {
246
247
return await axios .get (" http://localhost:4000/superheroes" );
247
248
};
248
249
249
- // data: AxiosResponse<Hero[]>
250
250
const { data, isLoading } = useQuery ({
251
251
queryKey: [" super-heroes" ],
252
252
queryFn: getAllSuperHero ,
@@ -407,7 +407,6 @@ const {
407
407
- fresh는 뜻 그대로 ` 신선한 ` 이라는 의미이다. 즉, 최신 상태라는 의미이다.
408
408
409
409
``` tsx
410
- // data: AxiosResponse<Hero[]>
411
410
const {
412
411
data,
413
412
// ...
@@ -443,7 +442,6 @@ const {
443
442
### refetchOnMount
444
443
445
444
``` tsx
446
- // data: AxiosResponse<Hero[]>
447
445
const {
448
446
data,
449
447
// ...
@@ -486,7 +484,6 @@ const {
486
484
### Polling
487
485
488
486
``` tsx
489
- // data: AxiosResponse<Hero[]>
490
487
const {
491
488
data,
492
489
// ...
@@ -518,7 +515,6 @@ react-query에서는 "refetchInterval", "refetchIntervalInBackground"을 이용
518
515
### enabled refetch
519
516
520
517
``` tsx
521
- // data: AxiosResponse<Hero[]>
522
518
const {
523
519
data,
524
520
refetch,
@@ -557,7 +553,6 @@ return (
557
553
### retry
558
554
559
555
``` tsx
560
- // data: AxiosResponse<Hero[]>
561
556
const {
562
557
data,
563
558
refetch,
@@ -587,15 +582,12 @@ const {
587
582
### select
588
583
589
584
``` tsx
590
- // data: string[]
591
585
const {
592
586
data,
593
- refetch,
594
587
// ...
595
588
} = useQuery ({
596
589
queryKey: [" super-heroes" ],
597
590
queryFn: getAllSuperHero ,
598
- // data: AxiosResponse<Hero[]>
599
591
select : (data ) => {
600
592
const superHeroNames = data .data .map ((hero ) => hero .name );
601
593
return superHeroNames ;
@@ -604,7 +596,7 @@ const {
604
596
605
597
return (
606
598
<div >
607
- { data .map ((heroName : string , idx : number ) => (
599
+ { data .map ((heroName , idx ) => (
608
600
<div key = { ` ${heroName }-${idx } ` } >{ heroName } </div >
609
601
))}
610
602
</div >
@@ -620,9 +612,8 @@ return (
620
612
### placeholderData
621
613
622
614
``` tsx
623
- const placeholderData = useMemo (() => generateFakeHeros (), []);
615
+ const placeholderData = useMemo (() => generateFakeHeroes (), []);
624
616
625
- // data: AxiosResponse<Hero[]>
626
617
const {
627
618
data,
628
619
// ...
@@ -653,7 +644,6 @@ const {
653
644
``` tsx
654
645
import { useQuery , keepPreviousData } from " @tanstack/react-query" ;
655
646
656
- // data: AxiosResponse<Hero[]>
657
647
const {
658
648
data,
659
649
// ...
@@ -669,7 +659,6 @@ const {
669
659
``` tsx
670
660
import { useQuery } from " @tanstack/react-query" ;
671
661
672
- // data: AxiosResponse<Hero[]>
673
662
const {
674
663
data,
675
664
// ...
@@ -687,7 +676,6 @@ const {
687
676
``` tsx
688
677
import { useQuery } from " @tanstack/react-query" ;
689
678
690
- // data: AxiosResponse<Hero[]>
691
679
const { data, dataUpdatedAt } = useQuery ({
692
680
queryKey: [" super-heroes" ],
693
681
queryFn: getAllSuperHero ,
@@ -710,13 +698,11 @@ const { data, dataUpdatedAt } = useQuery({
710
698
- [ useQueries 공식 문서] ( https://tanstack.com/query/v5/docs/react/reference/useQueries )
711
699
712
700
``` tsx
713
- // data: AxiosResponse<Hero[]>
714
701
const { data : superHeroes } = useQuery ({
715
702
queryKey: [" super-heroes" ],
716
703
queryFn: getAllSuperHero ,
717
704
});
718
705
719
- // data: AxiosResponse<Friend[]>
720
706
const { data : friends } = useQuery ({
721
707
queryKey: [" friends" ],
722
708
queryFn: getFriends ,
@@ -783,7 +769,6 @@ const combinedQueries = useQueries({
783
769
784
770
``` tsx
785
771
// 사전에 완료되어야 할 쿼리
786
- // data: AxiosResponse<User>
787
772
const { data : user } = useQuery ({
788
773
queryKey: [" user" , email ],
789
774
queryFn : () => getUserByEmail (email ),
@@ -792,7 +777,6 @@ const { data: user } = useQuery({
792
777
const channelId = user ?.data .channelId ;
793
778
794
779
// user 쿼리에 종속 쿼리
795
- // data: AxiosResponse<Course[]>
796
780
const { data : courses } = useQuery ({
797
781
queryKey: [" courses" , channelId ],
798
782
queryFn : () => getCoursesByChannelId (channelId ),
@@ -930,7 +914,6 @@ const fetchColors = async ({
930
914
return await axios .get (` http://localhost:4000/colors?page=${pageParam } ` );
931
915
};
932
916
933
- // data: InfiniteData<AxiosResponse<PaginationColors>, number>
934
917
const InfiniteQueries = () => {
935
918
const { data, hasNextPage, isFetching, isFetchingNextPage, fetchNextPage } =
936
919
useInfiniteQuery ({
@@ -1426,15 +1409,13 @@ const fetchGroups = async (): Promise<{ data: Group[] }> => {
1426
1409
};
1427
1410
1428
1411
// as-is
1429
- // data: Group[] | undefined
1430
1412
const { data } = useQuery ({
1431
1413
queryKey: [" groups" ],
1432
1414
queryFn: fetchGroups ,
1433
1415
select : (data ) => data .data ,
1434
1416
});
1435
1417
1436
1418
// to-be
1437
- // data: Group[]
1438
1419
const { data } = useSuspenseQuery ({
1439
1420
queryKey: [" groups" ],
1440
1421
queryFn: fetchGroups ,
@@ -1542,9 +1523,9 @@ const { data } = useQuery<
1542
1523
AxiosResponse<Hero[]>,
1543
1524
AxiosError,
1544
1525
string[],
1545
- ["super-heros ", number]
1526
+ ["super-heroes ", number]
1546
1527
>({
1547
- queryKey: ["super-heros ", id],
1528
+ queryKey: ["super-heroes ", id],
1548
1529
queryFn: getSuperHero,
1549
1530
select: (data) => {
1550
1531
const superHeroNames = data.data.map((hero) => hero.name);
@@ -1599,12 +1580,12 @@ const onClick = () => {
1599
1580
1600
1581
/**
1601
1582
주요 타입
1602
- * data: ` Todo `
1603
- * error: ` AxiosError <any , any >`
1604
- * onSuccess: ` (res : Todo , id : number , nextId : number )`
1605
- * onError: ` (err : AxiosError , id : number , nextId : number )`
1606
- * onMutate: ` (id : number )`
1607
- * onSettled: ` (res : Todo , err : AxiosError , id : number , nextId : number )` ,
1583
+ * data: Todo
1584
+ * error: AxiosError<any, any>
1585
+ * onSuccess: (res: Todo, id: number, nextId: number)
1586
+ * onError: (err: AxiosError, id: number, nextId: number)
1587
+ * onMutate: (id: number)
1588
+ * onSettled: (res: Todo, err: AxiosError, id: number, nextId: number),
1608
1589
*/
1609
1590
` ` `
1610
1591
@@ -1629,7 +1610,12 @@ export function useInfiniteQuery<
1629
1610
` ` `
1630
1611
1631
1612
` ` ` tsx
1632
- const { data, hasNextPage, fetchNextPage } = useInfiniteQuery<
1613
+ const {
1614
+ data,
1615
+ hasNextPage,
1616
+ fetchNextPage,
1617
+ //...
1618
+ } = useInfiniteQuery<
1633
1619
AxiosResponse<PaginationColors>,
1634
1620
AxiosError,
1635
1621
InfiniteData<AxiosResponse<PaginationColors>, number>,
@@ -1646,9 +1632,9 @@ const { data, hasNextPage, fetchNextPage } = useInfiniteQuery<
1646
1632
1647
1633
/**
1648
1634
주요 타입
1649
- * data: InfiniteData<AxiosResponse<PaginationColors>, number>
1635
+ * data: InfiniteData<AxiosResponse<PaginationColors>, number> | undefined
1650
1636
* error: AxiosError<any, any>
1651
- * select: (data: InfiniteData<CommunityArticlesResponse , number>): InfiniteData<CommunityArticlesResponse , number>
1637
+ * select: (data: InfiniteData<AxiosResponse<PaginationColors> , number>): InfiniteData<AxiosResponse<PaginationColors> , number>
1652
1638
* getNextPageParam: GetNextPageParamFunction<number, AxiosResponse<PaginationColors>>
1653
1639
*/
1654
1640
` ` `
0 commit comments