I have been using Angular 1 for a while now and am now starting to learn React but cannot figure out the main difference between the two. Seems like React is all about creating components, but can't we do the same in Angular using directives? I'm still at the very beginner level of react (learning through codeschool).
It would be very helpful if someone could provide one case and show me how to solve it using angular 1 and then react.
Below is what I've done in React so far (creating a CommentBox component that will display comments from the Comment component). How could I do this in angular 1 for example so I can see the difference.
CommentBox Component:
class CommentBox extends React.Component {
render() {
const comments = this._getComments() || [];
return(
<div className="comment-box">
<h3>Comments</h3>
<div className="comment-list">
{comments}
</div>
</div>
);
}
_getComments() {
const commentList = [
{ id: 1, author: 'Clu', body: 'Just say no to love!', avatarUrl: 'images/default-avatar.png' },
{ id: 2, author: 'Anne Droid', body: 'I wanna know what love is...', avatarUrl: 'images/default-avatar.png' }
];
return commentList.map((comment) => {
return (<Comment
author={comment.author}
body={comment.body}
avatarUrl={comment.avatarUrl}
key={comment.id} />);
});
}
}
Comment Component:
class Comment extends React.Component {
render() {
return(
<div className="comment">
<img src={this.props.avatarUrl} alt={`${this.props.author}'s picture`} />
<p className="comment-header">{this.props.author}</p>
<p className="comment-body">
{this.props.body}
</p>
</div>
);
}
}