Skip to content

Instantly share code, notes, and snippets.

@Harshakvarma
Created January 15, 2018 06:42
Show Gist options
  • Select an option

  • Save Harshakvarma/272572db3272f86bcccd53a4008f224e to your computer and use it in GitHub Desktop.

Select an option

Save Harshakvarma/272572db3272f86bcccd53a4008f224e to your computer and use it in GitHub Desktop.
Axios react API subreddit demo
import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class FetchDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentDidMount() {
axios.get(`http://www.reddit.com/r/${this.props.subreddit}.json`)
.then(res => {
const posts = res.data.data.children.map(obj => obj.data);
this.setState({ posts });
});
}
render() {
return (
<div>
<h1>{`/r/${this.props.subreddit}`}</h1>
<ul>
{this.state.posts.map(post =>
<li key={post.id}>{post.title}</li>
)}
</ul>
</div>
);
}
}
ReactDOM.render(
<FetchDemo subreddit="reactjs"/>,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment