渲染列表评论

1判断列表数据的长度是否为0

2如果为0 则渲染暂无评论

//导入react
import React from 'react'
import ReactDOM from 'react-dom'
//导入组件
// 约定1:类组件必须以大写字母开头
// 约定2:类组件应该继承react.component父类 从中可以使用父类的方法和属性
// 约定3:组件必须提供render方法
// 约定4:render方法必须有返回值
 
class HelloWorld extends React.Component {
    //初始化状态
    state={
        // comments:[{
        //     id:1,name:"geyao",content:"哈哈"
        // },{
        //     id:2,name:"fangfang",content:"哈哈"
        // },{
        //     id:3,name:"geyao",content:"哈哈"
        // }]
        comments:[]
    }
    render() {
        return (
            <div className="app">
                <div>
                    <input
                        className="user"
                        type="text"
                        placeholder="请输入评论人"
                    />
                    <br />
                    <textarea
                        className="content"
                        cols="30"
                        rows="10"
                        placeholder="请输入评论列表"
                    ></textarea>
                    <br />
                    <button>发表评论</button>
                </div>
                {this.state.comments.length===0?(
                        <div className="no-comment">暂无评论,快去评论吧~</div>
                ):(
                    <ul>
                    {/* <li>
                        <h3>评论人:jack</h3>
                        <h3>评论内容:沙发</h3>
                    </li> */}
                    {this.state.comments.map(item=>(
                        <li key={item.key}>
                        <h3>评论人:{item.name}</h3>
                        <p>评论内容:{item.content}</p>
                    </li>
                    )
                      
                        
                    )}
                </ul>
                )}
            
            
            </div>
        )
    }
}
 
ReactDOM.render(<HelloWorld />, document.getElementById('root'))
运行结果

最新文章

  1. 【Git】自定义Git
  2. 【总结】IE和Firefox的Javascript兼容性总结(转)
  3. Clarkson不等式
  4. CentOS添加swap分区
  5. Apriori算法
  6. hdu1166(线段树)
  7. js getByClass函数封装
  8. CentOS 6.5安装PostgreSQL9.3.5时报错: jade: Command not found
  9. 原生Jdbc操作Mysql数据库开发步骤
  10. swift 编写欢迎界面-- ios开发
  11. JS Bootstrap-DateRangePicker 如何设置默认值为空
  12. 前后端同学,必会的Linux常用基础命令
  13. 【Linux】配置SSH Key到GitHub/GitLab
  14. Chapter 5 Blood Type——24
  15. Exp3 免杀原理与实践 20164303 景圣
  16. LeetCode - Unique Email Addresses
  17. MyBatis在Oracle中插入数据并返回主键的问题解决
  18. 使用MYSQL的INNODB实现任务分发机制
  19. 1 CRM需求分析,数据库表,录入数据
  20. [洛谷P4340][SHOI2016]随机序列

热门文章

  1. (2)_引言Introduction【论文写作】
  2. H5本地存储:sessionStorage和localStorage
  3. jdbc连接MySQL数据库+简单实例(普通JDBC方法实现和连接池方式实现)
  4. mysql数据乱码
  5. uni-app中 未收藏和已收藏功能展示
  6. C# 将PDF转为Excel
  7. linux部署项目访问mysql问题
  8. 超详细讲解H5移动端适配
  9. golang内存对齐分析(转载)
  10. 公私钥 SSH 数字证书