Skip to content

Instantly share code, notes, and snippets.

@EQuimper
Created January 7, 2017 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EQuimper/9bb14d0c5a5ac1fc458bf4e5270fd451 to your computer and use it in GitHub Desktop.
Save EQuimper/9bb14d0c5a5ac1fc458bf4e5270fd451 to your computer and use it in GitHub Desktop.
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