React04---评论案例

效果图:
在这里插入图片描述
在这里插入图片描述
src结构:
在这里插入图片描述

index.html:
在这里插入图片描述
index.js:
在这里插入图片描述
comment-add.jsx:
在这里插入图片描述
comment-item.jsx:
在这里插入图片描述
comment-list.jsx:
在这里插入图片描述
app.jsx:

import React,{Component} from 'react'
import ReactDOM from 'react-dom'
import CommentAdd from './CommentAdd/comment-add'
import CommentList from './CommentList/comment-list'
export default class App extends Component{
    constructor(props){
        super(props);
        this.state={
            comments:[]
        };
    };
    componentDidMount(){
        setTimeout(() => {
            this.setState({
                comments:[
                        {username:'Tom',content:'React挺好的'},
                        {username:'Mary',content:'React挺好的'}
                      ]
              })
        },1000);
    }
    appTakeData = (comment) => {
        let {comments} = this.state;
        comments.unshift(comment);//unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度
        this.setState({comments});
    }
    appDeleteData = (index) =>{
        console.log(index);
        let {comments} = this.state;
        comments.splice(index,1);//splice()可以做到增删改
        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 appTakeData={this.appTakeData}></CommentAdd>
                <CommentList comments={comments} appDeleteData={this.appDeleteData}></CommentList>
            </div>
        </div>
       )
    }
}

问题总结
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值