REACT - axios

KIMMY·2020년 4월 10일
0

codingEveryday

목록 보기
6/8

설치 : npm i axios

한 api에서 여러 항목을 가져오는 경우 api.js파일을 하나 쓰는게 좋다. api 주소 앞부분은 동일하기 때문.
예) themoviedb에서 movie-인기/개봉/개봉예정..

Axios 장점: instance를 configure(설정) 할 수 있다. 그래서 baseurl/headers 등을 반복할 필요 없음.

Creating an instance

You can create a new instance of axios with a custom config.

axios.create([config])
const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}
});

예시!

import axios from "axios";
const api = axios.create({
	baseURL: "https://api.themoviedb.org/3/",
    params: {
    	api_key : "apiKey",
        language : "en-US"
    }
    
});

api.get("tv/popular");

이렇게 하면

https://api.themoviedb.org/3/tv/popular?api_key=99999999&language=en-US&page=1

으로 불러옴!

profile
SO HUMAN

0개의 댓글