React| 组件化 | 递归 | 生成唯一ID

    需要探究一下 .find() 和 findIndex() 的区别
import React from  'react'
import './commentsDemo.css' class CommentsDemo extends React.Component{
// 初始化状态
state = {
comments: [
{ id: 1, name: 'cnyangx', content: '前排出售瓜子、汽水、辣条!!!' },
{ id: 2, name: '大脸猫', content: '沙发~' },
{ id: 3, name: '老张', content: '我有酒,你有故事吗' }
],
userName: '',
userContent: ''
};
handleForm = (e) => {
this.setState({
[e.target.name] : e.target.value
})
};
// 验证id唯一性
verifyID = () => {
const newID = Math.floor(Math.random() * 100000);
const flag = this.state.comments.find( item => item.id === newID);
console.log(flag)
if(flag === -1){
this.verifyID()
}
return newID;
};
// 发表评论
publishComments = () => {
const {comments, userName, userContent} = this.state;
if(userName.trim()==='' || userContent.trim() === ''){
alert('请输入内容');
return
}
// 将新的评论添加到集合中
const newComments = [
{
id: this.verifyID(),
name: userName,
content: userContent
},
...comments
];
console.log(newComments);
this.setState({
comments: newComments
})
};
renderList = () => {
return this.state.comments.length === 0 ?
(<div className="no-comments">暂无评论,快去抢沙发吧!</div>) :
(
<ul>
{
this.state.comments.map(item => (
<li key={item.id}>
<h4>评论人: {item.name}</h4>
<p>评论内容:{item.content}</p>
</li>
))
}
</ul>
)
};
render() {
const {userName, userContent} = this.state;
return (
<div className="app">
<div>
<input type="text"
name="userName"
value={userName}
onChange={this.handleForm}
className="user"
placeholder="请输入评论人"/>
<br/>
<textarea name="userContent"
className="content"
value={userContent}
onChange={this.handleForm}
cols="30" rows="10"
placeholder="请输入评论内容">
</textarea>
<br/>
<button onClick={this.publishComments}>发表评论</button>
</div>
{/* 渲染评论列表 使用三元表达式 */}
{ this.renderList() }
</div>
)
}
} // 导出
export default CommentsDemo;
    使用ReactDOM.render( <CommentsDemo />, document.getElementByID('root))
渲染组件

【 React -- 2/100 】使用React实现评论功能的更多相关文章

  1. 【React 8/100】 React路由

    React路由 React路由介绍 现代的前端应用大多数是SPA(单页应用程序),也就是只有一个HTML页面的应用程序.因为它的用户体验更好.对服务器压力更小,所以更受欢迎.为了有效的使用单个页面来管 ...

  2. 【React 6/100】 React原理 | setState | JSX语法转换 | 组件更新机制

    ****关键字 | setState | JSX语法转换 | 组件更新机制 组件更新机制 setState() 的两个作用 修改state 更新组件 过程:父组件重新渲染时,也会重新渲染子组件,但只会 ...

  3. 【 React - 1/100 】React绑定事件this指向问题--改变state中的值

    /** * 报错: * Cannot read property 'setState' of undefined * 原因: this指向不一致.btnAddCount中的this 和render中的 ...

  4. React.js 小书 Lesson25 - 实战分析:评论功能(四)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson25 转载请注明出处,保留原文链接和作者信息. (本文未审核) 目前为止,第二阶段知识已经基本 ...

  5. React.js 小书 Lesson16 - 实战分析:评论功能(三)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson16 转载请注明出处,保留原文链接和作者信息. 接下来的代码比较顺理成章了.修改 Commen ...

  6. React.js 小书 Lesson14 - 实战分析:评论功能(一)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson14 转载请注明出处,保留原文链接和作者信息. 课程到这里大家已经掌握了 React.js 的 ...

  7. React+后端实现导出Excle表格的功能

    最近在做一个基于React+antd前端框架的Excel导出功能,我主要在后端做了处理,根据以下步骤,可以很容易就实现导出Excel表格数据的功能. 在做这类导出文件的功能,其实,在后端进行处理,会更 ...

  8. React Editor 应用编辑器(1) - 拖拽功能剖析

    这是可视化编辑器 Gaea-Editor 的第一篇连载分析文章,希望我能在有限的篇幅讲清楚制作这个网页编辑器的动机,以及可能带来的美好使用前景(画大饼).它会具有如下几个特征: 运行在网页 文档流布局 ...

  9. 4-13 Webpacker-React.js; 用React做一个下拉表格的功能: <详解>

    Rails5.1增加了Webpacker: Webpacker essentially is the decisions made by the Rails team and bundled up i ...

  10. React-Native(三):React Native是基于React设计的

    React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...

随机推荐

  1. SpringBoot2.2发行版新特性

    Spring Framework升级 SpringBoot2.2的底层Spring Framework版本升级为5.2. JMX默认禁用 默认情况下不再启用JMX. 可以使用配置属性spring.jm ...

  2. 关于SSD和YOLO对小目标的思考

    所谓的小目标,要看是绝对小目标(像素),和相对小目标(相对原图的长宽来看的).大目标小目标只跟receptive field(感受野)有关,cnn本身可以检测任何尺度的物体.ssd对小目标检测不太适用 ...

  3. Tensorflow2.0变化

    https://baijiahao.baidu.com/s?id=1627307436158652578&wfr=spider&for=pc https://zhidao.baidu. ...

  4. Python黑科技:FuckIt.py

    说起 Python 强大的地方,你可能想到是它的优雅.简洁.开发速度快,社区活跃度高.但真正使得这门语言经久不衰的一个重要原因是它的无所不能,因为社区有各种各样的第三库,使得我们用Python实现一个 ...

  5. AVLTree的实现以及左右旋转维持自平衡

    AVL(Adelson-Velskii and Landis)树是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它保证树的深度须是o(logN).最简单的想法是要求左右子树具有相同的高度, ...

  6. python调用c++类方法(2)

    testpy.cpp: #include<iostream> #include<vector> struct point{ float pointx; float pointy ...

  7. Mybaits多个参数的传递

    今天介绍是多个参数传递到映射xml,进行CURD操作 一.使用参数映射的方法进行传递 1在接口写对应的方法 public interface EmployeeMapper { public Emplo ...

  8. A 内存挂 B 封包挂 C 钩子挂 D CALL挂 外挂

    https://www.zhihu.com/question/32291769 作者:猎狐链接:https://www.zhihu.com/question/32291769/answer/70929 ...

  9. deepfm代码参考

    https://github.com/lambdaji/tf_repos/blob/master/deep_ctr/Model_pipeline/DeepFM.py https://www.cnblo ...

  10. lgb参数及调参

    1 参数含义 max_depth: 设置树的最大深度,默认为-1,即不限制最大深度,它用于限制过拟合 num_leave: 单颗树的叶子数目,默认为31 eval_metric: 评价指标,可以用lg ...