Created
January 7, 2017 01:01
-
-
Save EQuimper/9bb14d0c5a5ac1fc458bf4e5270fd451 to your computer and use it in GitHub Desktop.
This file contains 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
const routes = ( | |
<Router history={browserHistory}> | |
<Route path="/"> | |
<Route path="/posts/:id" component={PostPage} /> | |
</Route> | |
</Router> | |
) | |
<button onClick={() => browserHistory.push(`/posts/${post.id}`)}></button> | |
class PostPage extends Component { | |
state = { post: null, loading: false, error: null } | |
componentWillMount() { | |
this.setState({ loading: true }); | |
axios.get(`http:localhost:3000/api/posts/${this.props.params.id}`) | |
.then( | |
res => this.setState({ post: res.post, loading: false }), | |
err => this.setState({ error: true, loading: false }); | |
) | |
} | |
render() { | |
if (this.state.loading) { return <h1>Loading...</h1>; } | |
else if (!this.state.loading) { | |
if (this.state.error) { return <h1>Error...</h1>} | |
return ( | |
<div> | |
<h1>{this.state.post.title}</h1> | |
</div> | |
); | |
} | |
} | |
} | |
router.route('/api/posts/:id', (req, res) => { | |
Posts.findById(req.params.id) | |
.then( | |
post => res.status(201).json({ post, success: true }), | |
err => res.status(422).json({ success: false, error: 'No Post find' }) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment