You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//- CommentBox// - CommentList// - Comment// - CommentFormclassCommentBoxextendsReact.Component{render(){return(<divclassName="commentBox"><h1>Comments</h1><CommentList/><CommentForm/></div>);}}classCommentListextendsReact.Component{render(){return(<divclassName="commentList"><Comment/><Comment/><Comment/></div>);}}classCommentextendsReact.Component{render(){return(<divclassName="commnet">
Hello, world! I am a comment.
</div>);}}classCommentFormextendsReact.Component{render(){return(<formclassName="commentForm"><textareaplaceholder="please input your point!"name="content"></textarea><br/><buttontype="submit">submit</button></form>);}}
demo3-component-talking-to-component
//- CommentBox// - CommentList// - Comment// - CommentFormvardata=[{author: "Pete Hunt",content: "this post is very nice.",id: "1"},{author: "Jordan Walke",content: "nice article",id: "2"}];classCommentBoxextendsReact.Component{commentSubmitHandle(comment){if(!comment.id){comment.id=this.props.data.length+1;}letnewData=this.props.data.push(comment);this.setState({data: newData});}render(){return(<divclassName="commentBox"><h1>Comments</h1><CommentListdata={this.props.data}/><CommentFormonCommentSubmit={this.commentSubmitHandle.bind(this)}/></div>);}}classCommentListextendsReact.Component{render(){varcommnets=this.props.data.map(function(comment){return(<Commentdata={comment}key={comment.id}/>);});return(<divclassName="commentList">{commnets}</div>);}}classCommentextendsReact.Component{render(){return(<divclassName="commnet"><span>{this.props.data.author}:</span><span>{this.props.data.content}</span></div>);}}classCommentFormextendsReact.Component{handleSubmit(e){e.preventDefault();console.log(arguments);letauthor=this.refs.author.value.trim();letcontent=this.refs.content.value.trim();if(!(author&&content))return;this.props.onCommentSubmit({author: author,content: content});this.refs.author.value='';this.refs.content.value='';return;}render(){return(<formclassName="commentForm"onSubmit={this.handleSubmit.bind(this)}><label>name:</label><inputplaceholder="input author name"ref="author"/><br/><label>content</label><textareaplaceholder="please input your point!"ref="content"></textarea><br/><buttontype="submit">submit</button></form>);}}
demo4-component-state
vardata=[{author: "Pete Hunt",content: "this post is very nice.",id: "1"},{author: "Jordan Walke",content: "nice article",id: "2"}];classCommentBoxextendsReact.Component{constructor(props){super(props);this.state={formShow: false};}commentSubmitHandle(comment){if(!comment.id){comment.id=this.props.data.length+1;}letnewData=this.props.data.push(comment);this.setState({data: newData});}showForm(){this.setState({formShow: true});}render(){varpartial;if(this.state.formShow){partial=<CommentFormonCommentSubmit={this.commentSubmitHandle.bind(this)}/>}else{partial=<buttononClick={this.showForm.bind(this)}>add comment</button>}return(<divclassName="commentBox"><h1>Comments</h1><CommentListdata={this.props.data}/>{partial}</div>);}}classCommentListextendsReact.Component{
...
}classCommentextendsReact.Component{
...
}classCommentFormextendsReact.Component{handleSubmit(e){letauthor=this.refs.author.value.trim();letcontent=this.refs.content.value.trim();if(!(author&&content))return;this.props.onCommentSubmit({author: author,content: content});this.refs.author.value='';this.refs.content.value='';return;}render(){return(<formclassName="commentForm"onSubmit={this.handleSubmit.bind(this)}><label>name:</label><inputplaceholder="input author name"ref="author"/><br/><label>content</label><textareaplaceholder="please input your point!"ref="content"></textarea><br/><buttontype="submit">submit</button></form>);}}