首页 > 其他分享 >好客租房45-react组件基础综合案例-6边界问题

好客租房45-react组件基础综合案例-6边界问题

时间:2023-02-10 15:38:51浏览次数:45  
标签:userName content name userContent 45 边界问题 comments react 评论


边界问题

//导入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: '哈哈',
},
],
userName: '',
userContent: '',
}
//修改表单元素
handleForm = (e) => {
const { name, value } = e.target
this.setState({
[name]: value,
})
}
//添加信息
addComment=()=>{
const { comments,userName, userContent } = this.state
const newComents=[{
id:Math.random(),
name:userName,
content:userContent
},...comments]
if(userName.trim()===""||!userContent.trim()===""){
alert("评论人和评论列表不能为空")
return false
}
this.setState({
comments:newComents,
userContent:"",
userName:""
})
}
render() {
const { userName, userContent } = this.state
return (
<div className="app">
<div>
<input
className="user"
type="text"
placeholder="请输入评论人"
value={userName}
name="userName"
onChange={this.handleForm}
/>
<br />
<textarea
className="content"
cols="30"
rows="10"
placeholder="请输入评论列表"
value={userContent}
name="userContent"
onChange={this.handleForm}
></textarea>
<br />
<button onClick={this.addComment}>发表评论</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.id}>
<h3>评论人:{item.name}</h3>
<p>评论内容:{item.content}</p>
</li>
))}
</ul>
)}
</div>
)
}
}

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


运行结果

好客租房45-react组件基础综合案例-6边界问题_前端

 

标签:userName,content,name,userContent,45,边界问题,comments,react,评论
From: https://blog.51cto.com/u_15460007/6049429

相关文章

  • 好客租房43-react组件基础综合案例-4获取评论信息
    获取评论信息1使用受控组件方式创建表单//导入reactimportReactfrom'react'importReactDOMfrom'react-dom'//导入组件//约定1:类组件必须以大写字母开头//约定2......
  • 好客租房46-react组件进阶目标
    1能够使用props接收数据2能够使用父子组件之间的通讯3能够实现兄弟组件之间的通讯4能够给组件添加props校验5能够说出生命周期常用的钩子函数6能够知道高阶组件的作用组件通......
  • react 捕获异常并处理
     //@ts-nocheckimportReactfrom'react'classErrorBoundaryextendsReact.Component{constructor(props){super(props);this.state={error:......
  • 数据结构45-链表removeAt方法实现
      ......
  • react18 startTransition过度任务 性能优化
     官方:React.startTransition 不提供 isPending 的标志。要跟踪过渡的待定状态,请参阅 React.useTransition。由于React.startTransition 不支持跟踪pending状态,则......
  • react笔记之添加删除购物车
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • react笔记之完成Counter组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • react笔记之引入FontAwesome
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • react笔记之项目准备
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • react笔记之完成meals组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......