Created
August 15, 2017 15:20
-
-
Save davetoxa/9a3df5f7c75bc921141255d36ea5ece5 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
import React, { Component } from 'react' | |
import 'whatwg-fetch' | |
import Post from './post' | |
import PostForm from './PostForm' | |
const BASE_URL = process.env.BASE_URL || 'http://localhost:3000/api' | |
export default class App extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { posts: [] } | |
this.addPost = this.addPost.bind(this) | |
} | |
componentDidMount() { | |
this.loadPosts() | |
} | |
loadPosts() { | |
fetch(`${BASE_URL}/posts`) | |
.then(res => { | |
return res.json() | |
}).then(posts => { | |
this.setState({ posts: posts }) | |
}).catch(err => { | |
console.log(err.toString()) | |
}) | |
} | |
addPost(data) { | |
fetch(`${BASE_URL}/posts`, { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(data) | |
}).then(res => { | |
return res.json() | |
}).then(post => { | |
this.setState({ posts: this.state.posts.concat([post]) }) | |
}).catch(err => { | |
console.log(err.toString()) | |
}) | |
} | |
render() { | |
return( | |
<div> | |
<h3>My first SPA application!</h3> | |
{this.state.posts.map((post) =>{ | |
return <Post key={post.id} id={post.id} title={post.title} username={post.username}/> | |
})} | |
<PostForm onPostSubmit={this.addPost} /> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment