상세 컨텐츠

본문 제목

[React] API 호출하기

React

by 래모 2022. 6. 13. 18:41

본문

axios

: 현재 가장 많이 사용되고 있는 자바스크립트 HTTP 클리이언트로, HTTP 요청을 Promise 기반으로 처리한다는 특징이 있다

 

설치방법

//yarn
yarn add axios

//npm
npm install axios

 

사용 방법

쓰는 방식은 간단함!

API주소의 json 을 받아올 함수에 다음과 같이 적으면 된다.

const getMovie = () => {
    axios.get(`https://yts.mx/api/v2/list_movies.json?minimum_rating=7.0&sort_by=year`)
        .then(response => {
            setMovie(response.data.movie);
            setLoading(false);
        })
}

노마드코더 리액트 영화 웹 서비스 코드를 참고 했다


참고로 다음 코드들도 다 적용됨!!

const getMovie = async() => {
    const json = await (
        await fetch(`https://yts.mx/api/v2/movie_details.json?movie_id=${id}`))
            .json();
    setMovie(json.data.movie);
    setLoading(false);
}

const getMovie = () => {
    const response = axios.get(`https://yts.mx/api/v2/movie_details.json?movie_id=${id}`);
    setMovie(response.data.data.movie);
    setLoading(false);
}

관련글 더보기