/* Using createClass */
const Heart = React.createClass({
getInitialState: function () {
return { hearted: false };
},
heart: function () {
this.setState({ hearted: true });
},
render: function () {
return <i className="fa fa-heart" onClick={this.heart}></i>
}
});
/* Using classes */
class Heart extends React.Component {
constructor (props) {
super(props);
this.state = { hearted: false };
}
heart () {
this.setState({ hearted: true });
}
render () {
return <i className="fa fa-heart" onClick={this.heart}></i>
}
});
class Posts extends React.Component {
constructor (props) {
super(props);
this.state = { posts:[] };
}
componentDidMount () {
axios.get('/api/posts')
.then(res => res.data)
.then(data => this.setState({
posts: data
});
}
render () {
return (
<div>
{
this.state.posts.map(post => {
return <h1>{post.content}</h1>
});
}
</div>
);
}
});