class App extends React.Component {
state = {
category: 'business',
items: [],
};
componentDidMount() {
this.fetchData();
}
fetchData() {
axios.get('https://newsapi.org/v2/top-headlines?country=us&category='+this.state.category)
.then(res => {
this.setState({ items: res.data.articles });
});
}
changeCategory = () => {
this.setState(
{ category: 'entertainment' },
this.fetchData,
);
};
render() {
return (
<div className='wrapper'>
<button onClick={this.changeCategory}>CLICK</button>
</div>
);
}
}