0
1

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 3 years have passed since last update.

Reactでjson形式のapiを読み込む

Posted at

動作環境

React 17.0.1
npm 7.5.2
OS catalina

二種類のやり方紹介

axiosを使って読み込む方法

まずaxiosをインストール

npm install axios

ソースコード


useEffect(() => {
    axios.get('https://jsonplaceholder.typicode.com/posts')
        .then(res => {
        setPosts(res.data)
    })
})

JavaScriptの fetchメソッドを使う方法


useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/posts', {method: 'GET'})
        .then(res => res.json())
        .then(data => {
            setPosts(data);
        })
    })
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?