react学习笔记–评论回复
使用react 实现 在网页中的评论回复的功能
如图
这个小功能是基于脚手架上开发的,目的是熟悉一下脚手架的用法。
图中的样式是直接使用的bootstrap样式。
图中这个功能可以拆分为 4个组件分别为app.jsx 父组件、comment-add.jsx添加评论组件、comment-list 显示评论组件、comment-item.jsx 动态添加每一条评论组件。
组件化编程 新手可先实现静态页面,再去实现动态交互。
入口 index.js
import React from 'react';
import ReactDOM from 'react-dom'
import App from './components/app/app'
ReactDOM.render(<App/>, document.getElementById('root'));
app.jsx 父组件
import React, { Component } from 'react';
import CommentAdd from '../comment-add/comment-add'
import CommentList from '../comment-list/comment-list'
export default class App extends Component {
//赋初值
state = {
comments:[
{username: 'Tom',content: 'asd'},
{username: 'Bob',content: 'oerhhfjks'},
]
}
//添加评论
addComment = (comment) =>{
const {comments} = this.state;
comments.unshift(comment);
this.setState({comments});
}
//删除评论
deleteComment = (index) =>{
const {comments} = this.state;
//splice删除和 添加
comments.splice(index,1);
this.setState({comments});
}
render() {
return (
<div>
<header className="site-header jumbotron">
<div className="container">
<div className="row">
<div className="col-xs-12">
<h1>请发表对React的评论</h1>
</div>
</div>
</div>
</header>
<div className="container">
<CommentAdd addComment={this.addComment}/>
<CommentList comments = {this.state.comments } deleteComment={this.deleteComment}/>
</div>
</div>
);
}
}
comment-add.jsx添加评论组件
import React,{Component} from 'react';
import PropTypes from 'prop-types';
export default class CommentAdd extends Component {
// eslint-disable-next-line no-useless-constructor
constructor(props){
super(props)
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
username:'',
content:''
}
}
static propTypes = {
addComment: PropTypes.func.isRequired
}
handleSubmit(){
//收集数据
const comment = this.state;
this.props.addComment(comment);
this.setState({
username:'',
content:''
})
}
// handleSubmit = () =>{
// }
handleUNChange = (event) =>{
const username = event.target.value;
this.setState({username});
}
handleCChangge = (event) =>{
const content = event.target.value;
this.setState({content});
}
render() {
const {username,content} = this.state;
return (
<div className="col-md-4">
<form className="form-horizontal">
<div className="form-group">
<label>用户名</label>
<input type="text" className="form-control" placeholder="用户名"
value={username} onChange ={this.handleUNChange}/>
</div>
<div className="form-group">
<label>评论内容</label>
<textarea className="form-control" rows="6" placeholder="评论内容"
value ={content} onChange={this.handleCChangge}></textarea>
</div>
<div className="form-group">
<div className="col-sm-offset-2 col-sm-10">
<button type="button" className="btn btn-default pull-right" onClick = {this.handleSubmit}>提交</button>
</div>
</div>
</form>
</div>
);
}
}
comment-list 显示评论组件
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './commentList.css'
import CommentItem from '../comment-item/comment-item';
export default class CommentList extends Component {
//给组件类指定属性
static propTypes = {
comments: PropTypes.array.isRequired,
deleteComment: PropTypes.func.isRequired
}
render() {
//comments靠props传递
const {comments} = this.props;
const display = comments.length ===0 ?'block' :'none';
return (
<div className="col-md-8">
<h3 className="reply">评论回复:</h3>
<h2 style={{display}}>暂无评论,点击左侧添加评论!!!</h2>
<ul className="list-group">
{
comments.map((comment,index) => <CommentItem comment = {comment} key={index}
deleteComment={this.props.deleteComment} index = {index}/>)
}
</ul>
</div>
);
}
}
// CommentList.propTypes = {
// comments:PropTypes.array.isRequired
// }
comment-item.jsx 动态添加每一条评论组件
/* eslint-disable no-script-url */
/* eslint-disable jsx-a11y/anchor-is-valid */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './commentItem.css';
export default class CommentItem extends Component {
//添加约束
static propTypes = {
comment: PropTypes.object.isRequired,
deleteComment: PropTypes.func.isRequired,
index: PropTypes.number.isRequired
}
hanleClick = () => {
const {comment,deleteComment,index} = this.props;
//提示
if(window.confirm(`确定删除${comment.username}的评论吗?`)){
deleteComment(index);
}
//确定删除
}
render() {
const {comment} = this.props;
return (
<li className="list-group-item">
<div className="handle">
<a href="javascript:;" onClick ={this.hanleClick}>删除</a>
</div>
<p className="user"><span >{comment.username}</span><span>说:</span></p>
<p className="centence">{comment.content}!</p>
</li>
);
}
}