Skip to content

Commit 23ee3f2

Browse files
committed
docs: v4 예제 개선
1 parent c558ddc commit 23ee3f2

File tree

2 files changed

+160
-169
lines changed

2 files changed

+160
-169
lines changed

README.md

+19-33
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ result.refetch;
242242

243243
```tsx
244244
// 실제 예제
245+
// 💡 queryFn의 반환 타입을 지정해주면 useQuery의 타입 추론이 원활합니다.
245246
const getAllSuperHero = async (): Promise<AxiosResponse<Hero[]>> => {
246247
return await axios.get("http://localhost:4000/superheroes");
247248
};
248249

249-
// data: AxiosResponse<Hero[]>
250250
const { data, isLoading } = useQuery({
251251
queryKey: ["super-heroes"],
252252
queryFn: getAllSuperHero,
@@ -407,7 +407,6 @@ const {
407407
- fresh는 뜻 그대로 `신선한`이라는 의미이다. 즉, 최신 상태라는 의미이다.
408408

409409
```tsx
410-
// data: AxiosResponse<Hero[]>
411410
const {
412411
data,
413412
// ...
@@ -443,7 +442,6 @@ const {
443442
### refetchOnMount
444443

445444
```tsx
446-
// data: AxiosResponse<Hero[]>
447445
const {
448446
data,
449447
// ...
@@ -486,7 +484,6 @@ const {
486484
### Polling
487485

488486
```tsx
489-
// data: AxiosResponse<Hero[]>
490487
const {
491488
data,
492489
// ...
@@ -518,7 +515,6 @@ react-query에서는 "refetchInterval", "refetchIntervalInBackground"을 이용
518515
### enabled refetch
519516

520517
```tsx
521-
// data: AxiosResponse<Hero[]>
522518
const {
523519
data,
524520
refetch,
@@ -557,7 +553,6 @@ return (
557553
### retry
558554

559555
```tsx
560-
// data: AxiosResponse<Hero[]>
561556
const {
562557
data,
563558
refetch,
@@ -587,15 +582,12 @@ const {
587582
### select
588583

589584
```tsx
590-
// data: string[]
591585
const {
592586
data,
593-
refetch,
594587
// ...
595588
} = useQuery({
596589
queryKey: ["super-heroes"],
597590
queryFn: getAllSuperHero,
598-
// data: AxiosResponse<Hero[]>
599591
select: (data) => {
600592
const superHeroNames = data.data.map((hero) => hero.name);
601593
return superHeroNames;
@@ -604,7 +596,7 @@ const {
604596

605597
return (
606598
<div>
607-
{data.map((heroName: string, idx: number) => (
599+
{data.map((heroName, idx) => (
608600
<div key={`${heroName}-${idx}`}>{heroName}</div>
609601
))}
610602
</div>
@@ -620,9 +612,8 @@ return (
620612
### placeholderData
621613

622614
```tsx
623-
const placeholderData = useMemo(() => generateFakeHeros(), []);
615+
const placeholderData = useMemo(() => generateFakeHeroes(), []);
624616

625-
// data: AxiosResponse<Hero[]>
626617
const {
627618
data,
628619
// ...
@@ -653,7 +644,6 @@ const {
653644
```tsx
654645
import { useQuery, keepPreviousData } from "@tanstack/react-query";
655646

656-
// data: AxiosResponse<Hero[]>
657647
const {
658648
data,
659649
// ...
@@ -669,7 +659,6 @@ const {
669659
```tsx
670660
import { useQuery } from "@tanstack/react-query";
671661

672-
// data: AxiosResponse<Hero[]>
673662
const {
674663
data,
675664
// ...
@@ -687,7 +676,6 @@ const {
687676
```tsx
688677
import { useQuery } from "@tanstack/react-query";
689678

690-
// data: AxiosResponse<Hero[]>
691679
const { data, dataUpdatedAt } = useQuery({
692680
queryKey: ["super-heroes"],
693681
queryFn: getAllSuperHero,
@@ -710,13 +698,11 @@ const { data, dataUpdatedAt } = useQuery({
710698
- [useQueries 공식 문서](https://tanstack.com/query/v5/docs/react/reference/useQueries)
711699

712700
```tsx
713-
// data: AxiosResponse<Hero[]>
714701
const { data: superHeroes } = useQuery({
715702
queryKey: ["super-heroes"],
716703
queryFn: getAllSuperHero,
717704
});
718705

719-
// data: AxiosResponse<Friend[]>
720706
const { data: friends } = useQuery({
721707
queryKey: ["friends"],
722708
queryFn: getFriends,
@@ -783,7 +769,6 @@ const combinedQueries = useQueries({
783769

784770
```tsx
785771
// 사전에 완료되어야 할 쿼리
786-
// data: AxiosResponse<User>
787772
const { data: user } = useQuery({
788773
queryKey: ["user", email],
789774
queryFn: () => getUserByEmail(email),
@@ -792,7 +777,6 @@ const { data: user } = useQuery({
792777
const channelId = user?.data.channelId;
793778

794779
// user 쿼리에 종속 쿼리
795-
// data: AxiosResponse<Course[]>
796780
const { data: courses } = useQuery({
797781
queryKey: ["courses", channelId],
798782
queryFn: () => getCoursesByChannelId(channelId),
@@ -930,7 +914,6 @@ const fetchColors = async ({
930914
return await axios.get(`http://localhost:4000/colors?page=${pageParam}`);
931915
};
932916

933-
// data: InfiniteData<AxiosResponse<PaginationColors>, number>
934917
const InfiniteQueries = () => {
935918
const { data, hasNextPage, isFetching, isFetchingNextPage, fetchNextPage } =
936919
useInfiniteQuery({
@@ -1426,15 +1409,13 @@ const fetchGroups = async (): Promise<{ data: Group[] }> => {
14261409
};
14271410

14281411
// as-is
1429-
// data: Group[] | undefined
14301412
const { data } = useQuery({
14311413
queryKey: ["groups"],
14321414
queryFn: fetchGroups,
14331415
select: (data) => data.data,
14341416
});
14351417

14361418
// to-be
1437-
// data: Group[]
14381419
const { data } = useSuspenseQuery({
14391420
queryKey: ["groups"],
14401421
queryFn: fetchGroups,
@@ -1542,9 +1523,9 @@ const { data } = useQuery<
15421523
AxiosResponse<Hero[]>,
15431524
AxiosError,
15441525
string[],
1545-
["super-heros", number]
1526+
["super-heroes", number]
15461527
>({
1547-
queryKey: ["super-heros", id],
1528+
queryKey: ["super-heroes", id],
15481529
queryFn: getSuperHero,
15491530
select: (data) => {
15501531
const superHeroNames = data.data.map((hero) => hero.name);
@@ -1599,12 +1580,12 @@ const onClick = () => {
15991580
16001581
/**
16011582
주요 타입
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),
16081589
*/
16091590
```
16101591

@@ -1629,7 +1610,12 @@ export function useInfiniteQuery<
16291610
```
16301611

16311612
```tsx
1632-
const { data, hasNextPage, fetchNextPage } = useInfiniteQuery<
1613+
const {
1614+
data,
1615+
hasNextPage,
1616+
fetchNextPage,
1617+
//...
1618+
} = useInfiniteQuery<
16331619
AxiosResponse<PaginationColors>,
16341620
AxiosError,
16351621
InfiniteData<AxiosResponse<PaginationColors>, number>,
@@ -1646,9 +1632,9 @@ const { data, hasNextPage, fetchNextPage } = useInfiniteQuery<
16461632
16471633
/**
16481634
주요 타입
1649-
* data: InfiniteData<AxiosResponse<PaginationColors>, number>
1635+
* data: InfiniteData<AxiosResponse<PaginationColors>, number> | undefined
16501636
* error: AxiosError<any, any>
1651-
* select: (data: InfiniteData<CommunityArticlesResponse, number>): InfiniteData<CommunityArticlesResponse, number>
1637+
* select: (data: InfiniteData<AxiosResponse<PaginationColors>, number>): InfiniteData<AxiosResponse<PaginationColors>, number>
16521638
* getNextPageParam: GetNextPageParamFunction<number, AxiosResponse<PaginationColors>>
16531639
*/
16541640
```

0 commit comments

Comments
 (0)