react axiosを使用してAPIからデータを取得して表示させたいのですが
thenにいかずcatchになって通信が失敗してしまいます。
下記のコードの何がいけないのでしょうか。
APIURLの直打ちではjsonが取得できます。
jsx
1import React from 'react'; 2import axios from "axios"; 3 4const APIURL = 'http://api.giphy.com/v1/gifs/search?q=cat&api_key=********&limit=5' 5 6class Newslist extends React.Component { 7 constructor(props) { 8 super(props) 9 this.state = { 10 news: [] 11 } 12 this.handleGetNews = this.handleGetNews.bind(this) 13 } 14 15 componentWillMount(){ 16 this.handleGetNews() 17 } 18 19 handleGetNews() { 20 axios 21 .get(APIURL) 22 .then((res) => { 23 const data = res.data 24 this.setState(this.state.news.push(data[0].url)) 25 }) 26 .catch(() => { 27 console.log('通信に失敗しました。'); 28 }) 29 } 30 31 render(){ 32 return( 33 <ul> 34 <li><img src={this.state.news[0]} alt="" /></li> 35 </ul> 36 ) 37 } 38 39} 40 41export default Newslist;
大変お手数ではございますがご教示頂ければ幸いです。
それでは宜しくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。