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 |
Tags
- ngrok설치
- next.js css
- API token
- github io
- typescript react
- ngrok실행
- SCSS
- react env
- input type=file
- 컨디셔널 렌더링
- dart 변수
- API 토큰
- bootstrap
- git lab
- react
- Git
- nextjs 설치
- react typescript
- There isn’t anything to compare
- npm styled-reset
- getModifierState
- icon
- github
- rewrites
- nextjs
- createGlobalStyle
- nextjs .env
- fetch
- CSS
- styled components
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 |