0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【ReactNative】API経由でデータを取得する

Posted at

①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);
}
};

⑥コンポーネントがマウントされた時に実行する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?