Created
November 14, 2018 17:42
-
-
Save hpjaj/0d63aa165423665e4e18b5c34845eff4 to your computer and use it in GitHub Desktop.
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
// app/javascript/components/Posts.js | |
import React from 'react' | |
import axios from 'axios' | |
class Posts extends React.Component { | |
state = { | |
posts: [] | |
}; | |
componentDidMount() { | |
axios | |
.get('/api/posts') | |
.then(response => { | |
this.setState({ posts: response.data.posts }); | |
}) | |
} | |
renderAllPosts = () => { | |
return( | |
<ul> | |
{this.state.posts.map(post => ( | |
<li key={post}>{post}</li> | |
))} | |
</ul> | |
) | |
} | |
render() { | |
return ( | |
<div> | |
{this.renderAllPosts()} | |
</div> | |
) | |
} | |
} | |
export default Posts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment