react小案例(B站案例)

案例说明:发表评论人和评论内容,并展示;

有评论时:(展示评论内容)

无评论时:(展示暂无评论)

 

//创建一个组件
import React from 'react';
class Thrus extends React.Component {
    state = {
        txt: '',
        content: '',
        songs: [
            // {
            //     id: 1, name: 'qqq', unname: 'ddd'
            // },
            // {
            //     id: 2, name: 'qqq', unname: 'ddd'
            // },
            // {
            //     id: 3, name: 'qqq', unname: 'ddd'
            // }
        ],
        nextid: 4
    }
    handleChange = (e) => {
        const target = e.target;
        const name = target.name;
        const value = target.value;
        this.setState({
            [name]: value
        })
    }
    handleSend = () => {
        if (this.state.txt === '' || this.state.content === '') {
            alert('请输入评论人和评论内容')
            return false;
        }
        const comment = this.state.songs;
        comment.push({
            id: this.state.nextid,
            name: this.state.txt,
            unname: this.state.content
        })
        this.state.nextid++
        this.setState({
            nextid: this.state.nextid,
            songs: comment
        })
        this.state.txt = '';
        this.state.content = '';
    }
    handleShow = () => {
        if (this.state.songs.length !== 0) {
            return (
                <div>
                    <ul>
                        {this.state.songs.map(item =>
                            <li key={item.id}>
                                <p>评论人:{item.name}</p>
                                <p>评论内容:{item.unname}</p>
                            </li>
                        )}
                    </ul>
                </div>
            )
        }
        return <div>暂无评论</div>
          

      
    }
    render() {
        return (
            <div>
                <input placeholder='评论人' value={this.state.txt} type='text' name='txt' onChange={this.handleChange} /><br />
                <textarea value={this.state.content} type='text' name='content' onChange={this.handleChange} placeholder='评论内容' ></textarea>
                <br />
                <button onClick={this.handleSend}>发送</button><br />
               <div>{this.handleShow()}</div>


            </div>
        )
    }
}
//导出
export default Thrus;

 上述代码为一个评论组件;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值