This is the first time I'm working with APIs using ReactJS. I know the basics of reactjs pretty well but I'm unable to read the response. I've a json response from youtube api
.
Initially i got
TypeError: items.map is not a function.
But then i realized that i might be getting an Object and not an array. I solved the problem by items: json.statistics. But now I'm getting other error i.e
TypeError: items is undefined
I just want to fetch 3 information viz viewCount, subscriberCount and videoCount. I've written this code
import React, {Component} from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
items: [],
isLoaded: false
}
}
componentDidMount() {
fetch('https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCOYqNBjmWF8j-PzI7ZPjt_w&key=AIzaSyAMeZQW6sUQgLdDTnMVeokfbcFcT2A9SuA')
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json.statistics
})
});
}
render() {
var {isLoaded, items} = this.state;
if(!isLoaded) {
return (<div>Loading...</div>)
}
else {
return (
<div>
<ul>
{
items.map(item => (
<li key={item.id}>
Total views: {item.viewCount}
Total subscribers: {item.subscriberCount}
Total videos: {item.videoCount}
</li>
))}
</ul>
</div>
)
}
}
}
export default App;
json.items.statistics?myitems: json.items.statistics. But still i'm getting that error. I even changed the name of my variable so that it doesn't coincide with any other varibale of json response.