Last active
August 18, 2016 15:44
-
-
Save pjhjohn/33b2d40c903895181720fcd3e205a5ba 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
/* 1. Using this.props.children */ | |
// containers/Post.js | |
<PostDetail key={this.props.post.id} {...this.props.post} > | |
{this.props.comments.map((comment) => <Comment key={comment.id} {...comment} />)} | |
</PostDetail> | |
// components/PostDetail.js | |
<div> | |
{this.props.children} | |
<div/> | |
/* 2. transfer using props */ | |
// containers/Post.js | |
<PostDetail key={this.props.post.id} comments={this.props.comments} {...this.props.post} /> | |
// components/PostDetail.js | |
let comments = this.props.comments.map((comment) => <Comment key={comment.id} {...comment} />); | |
<div> | |
{comments} | |
</div> | |
/* 3. Embed comments into post */ | |
// containers/Post.js | |
function mapStateToProps(state) { | |
return { | |
post: Object.assign(state.post.detail, {comments: state.comment.list}) | |
}; | |
} | |
// in render | |
<PostDetail key={this.props.post.id} {...this.props.post} /> | |
// components/PostDetail.js | |
let comments = this.props.comments.map((comment) => <Comment key={comment.id} {...comment} />); | |
<div> | |
{comments} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment