/* Good practices: - name CSS classNamees like the associated component - prefix non-React method names by an "_" - pass a "key" attribute to elements in a loop State - direct reads, write through setState() - declare initial state in constructo() JSX: - "className", not "class" - knows how to render an array of JSX elements, not just one. */ const commentList = [ { id: 1, author: "Morgan McCircuit", body: "great picture!" }, { id: 2, author: "Bending Bender", body: "Excellent stuff" } ]; class Comment extends React.Component { render() { return (

{this.props.author}

{this.props.body}

Delete comment
); } } class CommentBox extends React.Component { constructor() { super(); this.state = { showComments: false }; } _getComments() { return commentList.map((comment) => { return (); }); } _getCommentsTitle(commentCount) { if (commentCount === 0) { return "No comments yet"; } else if (commentCount === 1) { return "1 comment"; } return `${commentCount} comments`; } _handleClick() { this.setState({ showComments: !this.state.showComments }); } render() { const comments = this._getComments(); let buttonText = "Show comments"; let commentNodes; if (this.state.showComments) { commentNodes =
{comments}
; buttonText = "Hide comments"; } return (

{this._getCommentsTitle(comments.length)}

{commentNodes}
); } } ReactDOM.render( , document.getElementById('comments-app') );