설치
yarn add axios
소스코드
import React, { useState } from 'react';
import axios from 'axios';
const News = () => {
const [data, setData] = useState(null);
// https://newsapi.org/s/south-korea-news-api
// https://newsapi.org/v2/top-headlines?country=kr&apiKey=a8a9fcfc5218454aaf0b97113bb4f94c
// https://newsapi.org/v2/top-headlines?country=kr&category=business&apiKey=a8a9fcfc5218454aaf0b97113bb4f94c
// category: business, entertainment, health, science, sports, technology
const onClick = async () => {
try {
const response = await axios.get(
'https://newsapi.org/v2/top-headlines?country=kr&apiKey=a8a9fcfc5218454aaf0b97113bb4f94c'
);
setData(response.data);
} catch (e) {
console.log(e);
}
};
return (
<div>
<button onClick={onClick}>불러오기</button>
{data && (
<textarea
rows={7}
value={JSON.stringify(data, null, 2)}
readOnly={true}
/>
)}
</div>
);
};
export default News;
'Web 개발 > React' 카테고리의 다른 글
[React] Context API 수정, 동적으로 사용하기 (useContext Hook, static contextType) (0) | 2020.02.11 |
---|---|
[React] UI 만들기 styled-components (0) | 2020.02.10 |
[React] REST API 개발 전에 JSONPlaceholder 이용해 JSON 데이터 샘플 호출해서 가져오기 (0) | 2020.02.10 |
[React] NavLink 를 이용해 현재 경로와 Link가 일치할때 Style을 주자 (+코드 예제) (0) | 2020.02.09 |
[React] Switch를 이용해 URL 접속시 존재하지 않는 페이지 (Not Found Page) 처리 방법 (+ 코드예제) (0) | 2020.02.09 |