Created
January 15, 2018 06:42
-
-
Save Harshakvarma/272572db3272f86bcccd53a4008f224e to your computer and use it in GitHub Desktop.
Axios react API subreddit demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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