Skip to content

Commit

Permalink
docs: type κ°œμ„ 
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 committed Apr 13, 2024
1 parent 6ba1958 commit c558ddc
Showing 1 changed file with 70 additions and 48 deletions.
118 changes: 70 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,11 @@ result.refetch;

```tsx
// μ‹€μ œ 예제
const getAllSuperHero = async () => {
const getAllSuperHero = async (): Promise<AxiosResponse<Hero[]>> => {
return await axios.get("http://localhost:4000/superheroes");
};

// data: AxiosResponse<Hero[]>
const { data, isLoading } = useQuery({
queryKey: ["super-heroes"],
queryFn: getAllSuperHero,
Expand All @@ -258,8 +259,12 @@ const { data, isLoading } = useQuery({

```tsx
// (1) queryKeyλŠ” 데이터λ₯Ό κ³ μœ ν•˜κ²Œ 식별에 더해 쿼리 ν•¨μˆ˜μ— μ•„λž˜μ™€ 같이 νŽΈλ¦¬ν•˜κ²Œ 전달할 μˆ˜λ„ μžˆλ‹€.
const getSuperHero = async ({ queryKey }: any) => {
const heroId = queryKey[1]; // queryKey: ["super-hero", "3"]
const getSuperHero = async ({
queryKey,
}: {
queryKey: ["super-hero", number];
}): Promise<AxiosResponse<Hero>> => {
const heroId = queryKey[1]; // ex) queryKey: ["super-hero", "3"]

return await axios.get(`http://localhost:4000/superheroes/${heroId}`);
};
Expand Down Expand Up @@ -296,7 +301,7 @@ useQuery({ queryKey: ["todo", 5, { preview: true }], ...})

```tsx
// (2) μƒλ‹¨μ˜ queryKey μ˜ˆμ œμ™€ λ°˜λŒ€λ‘œ queryFn 자체적으둜 인자λ₯Ό λ°›λŠ” ν˜•νƒœ
const getSuperHero = async (heroId: string) => {
const getSuperHero = async (heroId: string): Promise<AxiosResponse<Hero>> => {
return await axios.get(`http://localhost:4000/superheroes/${heroId}`);
};

Expand Down Expand Up @@ -402,6 +407,7 @@ const {
- freshλŠ” 뜻 κ·ΈλŒ€λ‘œ `μ‹ μ„ ν•œ`μ΄λΌλŠ” μ˜λ―Έμ΄λ‹€. 즉, μ΅œμ‹  μƒνƒœλΌλŠ” μ˜λ―Έμ΄λ‹€.

```tsx
// data: AxiosResponse<Hero[]>
const {
data,
// ...
Expand Down Expand Up @@ -437,6 +443,7 @@ const {
### refetchOnMount

```tsx
// data: AxiosResponse<Hero[]>
const {
data,
// ...
Expand Down Expand Up @@ -479,6 +486,7 @@ const {
### Polling

```tsx
// data: AxiosResponse<Hero[]>
const {
data,
// ...
Expand Down Expand Up @@ -510,6 +518,7 @@ react-queryμ—μ„œλŠ” "refetchInterval", "refetchIntervalInBackground"을 이용
### enabled refetch

```tsx
// data: AxiosResponse<Hero[]>
const {
data,
refetch,
Expand Down Expand Up @@ -548,6 +557,7 @@ return (
### retry

```tsx
// data: AxiosResponse<Hero[]>
const {
data,
refetch,
Expand Down Expand Up @@ -577,15 +587,17 @@ const {
### select

```tsx
// data: string[]
const {
data,
refetch,
// ...
} = useQuery({
queryKey: ["super-heroes"],
queryFn: getAllSuperHero,
// data: AxiosResponse<Hero[]>
select: (data) => {
const superHeroNames = data.data.map((hero: Data) => hero.name);
const superHeroNames = data.data.map((hero) => hero.name);
return superHeroNames;
},
});
Expand All @@ -610,6 +622,7 @@ return (
```tsx
const placeholderData = useMemo(() => generateFakeHeros(), []);

// data: AxiosResponse<Hero[]>
const {
data,
// ...
Expand Down Expand Up @@ -640,6 +653,7 @@ const {
```tsx
import { useQuery, keepPreviousData } from "@tanstack/react-query";

// data: AxiosResponse<Hero[]>
const {
data,
// ...
Expand All @@ -655,6 +669,7 @@ const {
```tsx
import { useQuery } from "@tanstack/react-query";

// data: AxiosResponse<Hero[]>
const {
data,
// ...
Expand All @@ -672,6 +687,7 @@ const {
```tsx
import { useQuery } from "@tanstack/react-query";

// data: AxiosResponse<Hero[]>
const { data, dataUpdatedAt } = useQuery({
queryKey: ["super-heroes"],
queryFn: getAllSuperHero,
Expand All @@ -694,11 +710,14 @@ const { data, dataUpdatedAt } = useQuery({
- [useQueries 곡식 λ¬Έμ„œ](https://tanstack.com/query/v5/docs/react/reference/useQueries)

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

// data: AxiosResponse<Friend[]>
const { data: friends } = useQuery({
queryKey: ["friends"],
queryFn: getFriends,
});
Expand Down Expand Up @@ -764,6 +783,7 @@ const combinedQueries = useQueries({

```tsx
// 사전에 μ™„λ£Œλ˜μ–΄μ•Ό ν•  쿼리
// data: AxiosResponse<User>
const { data: user } = useQuery({
queryKey: ["user", email],
queryFn: () => getUserByEmail(email),
Expand All @@ -772,6 +792,7 @@ const { data: user } = useQuery({
const channelId = user?.data.channelId;

// user 쿼리에 쒅속 쿼리
// data: AxiosResponse<Course[]>
const { data: courses } = useQuery({
queryKey: ["courses", channelId],
queryFn: () => getCoursesByChannelId(channelId),
Expand Down Expand Up @@ -901,12 +922,15 @@ const prefetchTodos = async () => {
import { useInfiniteQuery } from "@tanstack/react-query";

// useInfiniteQuery의 queryFn의 λ§€κ°œλ³€μˆ˜λŠ” `pageParam`μ΄λΌλŠ” ν”„λ‘œνΌν‹°λ₯Ό κ°€μ§ˆ 수 μžˆλ‹€.
const fetchColors = async ({ pageParam }) => {
return await axios.get(
`http://localhost:4000/colors?_limit=2&_page=${pageParam}`
);
const fetchColors = async ({
pageParam,
}: {
pageParam: number;
}): Promise<AxiosResponse<PaginationColors>> => {
return await axios.get(`http://localhost:4000/colors?page=${pageParam}`);
};

// data: InfiniteData<AxiosResponse<PaginationColors>, number>
const InfiniteQueries = () => {
const { data, hasNextPage, isFetching, isFetchingNextPage, fetchNextPage } =
useInfiniteQuery({
Expand Down Expand Up @@ -984,30 +1008,35 @@ const InfiniteQueries = () => {
const { data, hasNextPage, isFetching, isFetchingNextPage, fetchNextPage } =
useInfiniteQuery({
queryKey: ["colors"],
queryFn: fetchColors,
initialPageParam: 1,
queryFn: ({ pageParam }) => fetchColors(pageParam), // pageParam: { page: number; etc: string }
initialPageParam: {
page: number,
etc: "hi",
},
getNextPageParam: (lastPage, allPages) => {
return (
allPages.length < 4 && {
page: allPages.length + 1,
etc: "hi",
etc: "bye",
};
)
},
});
```

- 그러면 `queryFn`에 넣은 pageParamsμ—μ„œ getNextPageParamμ—μ„œ λ°˜ν™˜ν•œ 객체λ₯Ό λ°›μ•„μ˜¬ 수 μžˆλ‹€.
- 그러면 `queryFn`에 넣은 pageParamμ—μ„œ getNextPageParamμ—μ„œ λ°˜ν™˜ν•œ 객체λ₯Ό λ°›μ•„μ˜¬ 수 μžˆλ‹€.

```tsx
/**
* pageParam
* { page, etc }
*/
const fetchColors = async ({ pageParam }) => {
const { page, etc } = pageParam;

return await axios.get(`http://localhost:4000/colors?_limit=2&_page=${page}`);
const fetchColors = async ({
page,
etc,
}: {
page: number;
etc: string;
}): Promise<AxiosResponse<PaginationColors>> => {
return await axios.get(
`http://localhost:4000/colors?page=${page}?etc=${etc}`
);
};
```

Expand Down Expand Up @@ -1510,10 +1539,10 @@ import { AxiosError } from "axios";
// useQuery νƒ€μž… 적용 μ˜ˆμ‹œ
const { data } = useQuery<
SuperHeros,
AxiosResponse<Hero[]>,
AxiosError,
SuperHeroName[],
[string, number]
string[],
["super-heros", number]
>({
queryKey: ["super-heros", id],
queryFn: getSuperHero,
Expand All @@ -1525,9 +1554,9 @@ const { data } = useQuery<
/**
μ£Όμš” νƒ€μž…
* data: `SuperHeroName[] | undefined`
* error: `AxiosError<any, any>`
* select: `(data: SuperHeros): SuperHeroName[]`
* data: string[] | undefined
* error: AxiosError<any, any>
* select: (data: AxiosResponse<Hero[]>): string[]
*/
```

Expand Down Expand Up @@ -1600,33 +1629,27 @@ export function useInfiniteQuery<
```

```tsx
const fetchColors = async ({ pageParam }) => {
const { page = 1, etc } = pageParam;
return await axios.get(`http://localhost:4000/colors?_limit=2&_page=${page}`);
};

const { data, hasNextPage, fetchNextPage } = useInfiniteQuery<
Colors,
AxiosResponse<PaginationColors>,
AxiosError,
Colors,
InfiniteData<AxiosResponse<PaginationColors>, number>,
["colors"]
>({
queryKey: ["colors"],
queryFn: ({ pageParam }) => fetchColors({ page: pageParam }),
initialPageParam: 0,
getNextPageParam: (lastPage) => {
queryFn: fetchColors,
initialPageParam: 1,
getNextPageParam: (lastPage, allPages) => {
return allPages.length < 4 && allPages.length + 1;
},
// ...options
});
/**
μ£Όμš” νƒ€μž…
* data: InfiniteData<ModelListResponse>
* error: `AxiosError<any, any>`
* select: InfiniteData<ModelListResponse>
* getNextPageParam: GetNextPageParamFunction<Colors>
* data: InfiniteData<AxiosResponse<PaginationColors>, number>
* error: AxiosError<any, any>
* select: (data: InfiniteData<CommunityArticlesResponse, number>): InfiniteData<CommunityArticlesResponse, number>
* getNextPageParam: GetNextPageParamFunction<number, AxiosResponse<PaginationColors>>
*/
```

Expand All @@ -1639,9 +1662,8 @@ const { data, hasNextPage, fetchNextPage } = useInfiniteQuery<
- κ°€μž₯ 쒋은 방법은 `queryFn`의 νƒ€μž…μ„ 잘 μ •μ˜ν•΄μ„œ `νƒ€μž… μΆ”λ‘ `이 μ›ν™œν•˜κ²Œ 되게 ν•˜λŠ” 것이닀.

```tsx
const fetchGroups = async (): Promise<{ data: Group[] }> => {
const res = await axios.get("/groups");
return res;
const fetchGroups = async (): Promise<AxiosResponse<Group[]> => {
return await axios.get("/groups");
};
const { data } = useQuery({
Expand All @@ -1652,9 +1674,9 @@ const { data } = useQuery({
/**
μ£Όμš” νƒ€μž…
* data: Group[] | undefined
* data: AxiosResponse<Group[]> | undefined
* error: Error | null
* select: (data: { data: Group[] }): Group[]
* select: (data: AxiosResponse<Group[]>): Group[]
*/
```

Expand Down

0 comments on commit c558ddc

Please sign in to comment.