Skip to content

Commit 3b1f58a

Browse files
authored
Merge pull request #9 from yunwoo-yu/master
feat: fetch 함수 async await 추가
2 parents f6d05d2 + 68e96b7 commit 3b1f58a

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ result.isLoading
249249

250250
```jsx
251251
// 실제 예제
252-
const getAllSuperHero = useCallback(() => {
253-
return axios.get("http://localhost:4000/superheroes");
254-
}, []);
252+
const getAllSuperHero = async () => {
253+
return await axios.get("http://localhost:4000/superheroes");
254+
};
255255

256256
const { data, isLoading } = useQuery(["super-heroes"], getAllSuperHero);
257257
```
@@ -264,10 +264,10 @@ const { data, isLoading } = useQuery(["super-heroes"], getAllSuperHero);
264264

265265
```jsx
266266
// (1)
267-
const getSuperHero = ({ queryKey }: any) => {
267+
const getSuperHero = async ({ queryKey }: any) => {
268268
const heroId = queryKey[1]; // queryKey: ['super-hero', '3']
269269

270-
return axios.get(`http://localhost:4000/superheroes/${heroId}`);
270+
return await axios.get(`http://localhost:4000/superheroes/${heroId}`);
271271
};
272272

273273
const useSuperHeroData = (heroId: string) => {
@@ -289,8 +289,8 @@ const useSuperHeroData = (heroId: string) => {
289289

290290
```jsx
291291
// (2)
292-
const getSuperHero = (heroId: string) => {
293-
return axios.get(`http://localhost:4000/superheroes/${heroId}`);
292+
const getSuperHero = async (heroId: string) => {
293+
return await axios.get(`http://localhost:4000/superheroes/${heroId}`);
294294
};
295295

296296
const useSuperHeroData = (heroId: string) => {
@@ -607,8 +607,10 @@ return (
607607
[목차 이동](#주요-컨셉-및-가이드-목차)
608608
609609
```jsx
610-
const fetchColors = (pageNum: number) => {
611-
return axios.get(`http://localhost:4000/colors?_limit=2&_page=${pageNum}`);
610+
const fetchColors = async (pageNum: number) => {
611+
return await axios.get(
612+
`http://localhost:4000/colors?_limit=2&_page=${pageNum}`
613+
);
612614
};
613615

614616
const { isLoading, isError, error, data, isFetching, isPreviousData } =
@@ -820,8 +822,10 @@ useEffect(() => {
820822
import { useInfiniteQuery } from "react-query";
821823
// import { useInfiniteQuery } from '@tanstack/react-query' v4
822824

823-
const fetchColors = ({ pageParam = 1 }) => {
824-
return axios.get(`http://localhost:4000/colors?_limit=2&_page=${pageParam}`);
825+
const fetchColors = async ({ pageParam = 1 }) => {
826+
return await axios.get(
827+
`http://localhost:4000/colors?_limit=2&_page=${pageParam}`
828+
);
825829
};
826830

827831
const InfiniteQueries = () => {
@@ -897,10 +901,10 @@ const { data } = useInfiniteQuery(["colors"], fetchColors, {
897901
* pageParam
898902
* { page, etc }
899903
*/
900-
const fetchColors = ({ pageParam }) => {
904+
const fetchColors = async ({ pageParam }) => {
901905
const { page = 1, etc } = pageParam;
902906

903-
return axios.get(`http://localhost:4000/colors?_limit=2&_page=${page}`);
907+
return await axios.get(`http://localhost:4000/colors?_limit=2&_page=${page}`);
904908
};
905909
```
906910
@@ -1265,10 +1269,10 @@ function App() {
12651269
12661270
```jsx
12671271
// 기본 쿼리 함수
1268-
const getSuperHero = ({ queryKey }: any) => {
1272+
const getSuperHero = async ({ queryKey }: any) => {
12691273
const heroId = queryKey[1];
12701274

1271-
return axios.get(`http://localhost:4000/superheroes/${heroId}`);
1275+
return await axios.get(`http://localhost:4000/superheroes/${heroId}`);
12721276
};
12731277

12741278
const queryClient = new QueryClient({

0 commit comments

Comments
 (0)