Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- github io
- icon
- dart 변수
- fetch
- rewrites
- npm styled-reset
- input type=file
- github
- styled components
- CSS
- react typescript
- There isn’t anything to compare
- react
- SCSS
- typescript react
- nextjs 설치
- ngrok설치
- ngrok실행
- nextjs
- nextjs .env
- git lab
- react env
- API 토큰
- bootstrap
- getModifierState
- createGlobalStyle
- 컨디셔널 렌더링
- API token
- next.js css
- Git
Archives
- Today
- Total
꾸준히 성장하는 개발자
[React] API를 이용한 data 가져오기 본문
1. then() 을 사용한 방법
useEffect(() => {
fetch(
`https://yts.mx/api/v2/list_movies.json?minimum_rating=8.8&sort_by=year`
)
.then((response) => response.json())
.then((json) => {
setMovies(json.data.movies);
setLoading(false); //로딩이 끝났으니 finish시켜주기 위해 진행
});
}, []);
2. async await 사용한 방법
const getMovies = async () => {
const response = await fetch(
`https://yts.mx/api/v2/list_movies.json?minimum_rating=8.8&sort_by=year`
);
const json = await response.json();
setMovies(json.data.movies);
setLoading(false);
};
useEffect(() => {
getMovies();
}, []);
3. 좀 더 짧게 하는 방법
const getMovies = async () => {
const json = await (
await fetch(
`https://yts.mx/api/v2/list_movies.json?minimum_rating=8.8&sort_by=year`
)
).json();
setMovies(json.data.movies);
setLoading(false);
};
useEffect(() => {
getMovies();
}, []);
console.log(movies);
'React' 카테고리의 다른 글
[React 라이브러리] 상태 관리 라이브러리 Redux (0) | 2022.05.20 |
---|---|
React with Typescript 설치 (0) | 2022.05.19 |
React - jsx , state, props (0) | 2022.05.06 |
[React] React.memo() (0) | 2022.05.04 |
[에러] Plugin "react" was conflicted between "package.json » eslint-config-react-app (3) | 2022.04.29 |