①APIのアカウントを取得する
②Axiosをインストール(ターミナルで)
③axiosをインポートする
④APIのエンドポイントを定義する
const URL =
http://newsapi.org/v2/top-headlines?country=jp&apiKey=APIキー
;
※ここではNewsAPIを使用
⑤APIを叩いて、中身を取得するメソッドの作成
//APIを叩いて、ニュースを取得するメソッド
//非同期の場合はasyncを用いる
const fetchArticles = async () => {
try {
const response = await axios.get(APIのURL);
setArticles(response.data.articles)
} catch (error) {
console.error(error);
}
};