|
@@ -4,6 +4,10 @@ Good practices:
|
|
- prefix non-React method names by an "_"
|
|
- prefix non-React method names by an "_"
|
|
- pass a "key" attribute to elements in a loop
|
|
- pass a "key" attribute to elements in a loop
|
|
|
|
|
|
|
|
+State
|
|
|
|
+- direct reads, write through setState()
|
|
|
|
+- declare initial state in constructo()
|
|
|
|
+
|
|
JSX:
|
|
JSX:
|
|
- "className", not "class"
|
|
- "className", not "class"
|
|
- knows how to render an array of JSX elements, not just one.
|
|
- knows how to render an array of JSX elements, not just one.
|
|
@@ -33,6 +37,13 @@ class Comment extends React.Component {
|
|
}
|
|
}
|
|
|
|
|
|
class CommentBox extends React.Component {
|
|
class CommentBox extends React.Component {
|
|
|
|
+ constructor() {
|
|
|
|
+ super();
|
|
|
|
+ this.state = {
|
|
|
|
+ showComments: false
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
_getComments() {
|
|
_getComments() {
|
|
return commentList.map((comment) => {
|
|
return commentList.map((comment) => {
|
|
return (<Comment
|
|
return (<Comment
|
|
@@ -52,15 +63,25 @@ class CommentBox extends React.Component {
|
|
return `${commentCount} comments`;
|
|
return `${commentCount} comments`;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ _handleClick() {
|
|
|
|
+ this.setState({
|
|
|
|
+ showComments: !this.state.showComments
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
render() {
|
|
render() {
|
|
const comments = this._getComments();
|
|
const comments = this._getComments();
|
|
|
|
+ let buttonText = "Show comments";
|
|
|
|
+ let commentNodes;
|
|
|
|
+ if (this.state.showComments) {
|
|
|
|
+ commentNodes = <div className="comment-list">{comments}</div>;
|
|
|
|
+ buttonText = "Hide comments";
|
|
|
|
+ }
|
|
return (
|
|
return (
|
|
<div className="comment-box">
|
|
<div className="comment-box">
|
|
- <h3>Comments</h3>
|
|
|
|
|
|
+ <button onClick={this._handleClick.bind(this)}>{buttonText}</button>
|
|
<h4 className="comment-count">{this._getCommentsTitle(comments.length)}</h4>
|
|
<h4 className="comment-count">{this._getCommentsTitle(comments.length)}</h4>
|
|
- <div className="comment-list">
|
|
|
|
- {comments}
|
|
|
|
- </div>
|
|
|
|
|
|
+ {commentNodes}
|
|
</div>
|
|
</div>
|
|
);
|
|
);
|
|
}
|
|
}
|