Skip to content

Instantly share code, notes, and snippets.

@pjhjohn
Last active August 18, 2016 15:44
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 pjhjohn/33b2d40c903895181720fcd3e205a5ba to your computer and use it in GitHub Desktop.
Save pjhjohn/33b2d40c903895181720fcd3e205a5ba to your computer and use it in GitHub Desktop.
/* 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