React复杂案例之评论

1.index

import React from 'react'
import ReactDOM from 'react-dom'
import App from './components/app/app'

/*

    1.划分组件
    2.静态组件
    3.交互组件

 */

ReactDOM.render(<App/>,document.getElementById('root'));

2.app


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{

    constructor(props){
        super(props);
    }

    //初始化数据
    state = {
        comments:[
            {username:'tom',content:'react is easy'},
            {username:'mary',content:'java is easy'},
            {username:'jack',content:'php is easy'},
        ]
    };

    //添加评论,传递给添加组件
    addComment = (comment)=>{
        const {comments} = this.state;
        comments.unshift(comment);
        this.setState({comments});
    };

    //删除指定评论
    deleteComment = (index)=>{
        const {comments} = this.state;
        comments.splice(index,1);
        this.setState({comments});
    };

    render(){

        const {comments} = this.state;

        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={comments} deleteComment={this.deleteComment}/>
                </div>
            </div>
        )
    }
}

3.add


import React,{Component} from 'react'
import PropTypes from 'prop-types'

export default class CommentAdd extends Component{

    static propTypes = {
        addComment:PropTypes.func.isRequired
    };

    state = {
      username:'',
      content:''
    };

    //处理提交事件
    handleSubmit = (e)=>{
        //收集数据
        const comment = this.state;
        //更新状态
        this.props.addComment(comment);
        //清楚输入数据
        this.setState({
            username:'',
            content:''
        });
    };

    handleUsernameChange = (e)=>{
        const username = e.target.value.trim();
        this.setState({username})
    };

    handleContentChange = (e)=>{
        const content = e.target.value.trim();
        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.handleUsernameChange}/>
                    </div>
                    <div className="form-group">
                        <label>评论内容</label>
                        <textarea className="form-control" rows="6" placeholder="评论内容" value={content} onChange={this.handleContentChange}/>
                    </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>
        )
    }
}

4.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(){

        const {comments,deleteComment} = 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} deleteComment={deleteComment} key={index} index={index}/>)
                    }
                </ul>
            </div>
        )
    }
}

5.item


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
    };

    handleDeleteClick = (e)=>{
        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.handleDeleteClick}>删除</a>
                </div>
                <p className="user"><span>{comment.username}</span><span>说:</span></p>
                <p className="centence">{comment.content}</p>
            </li>
        )
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生糖葫芦侠

创作不易,请多多支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值