Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Js—react 评论页面

zhuanzhudeyipi 2019-12-30 14:53:04 阅读数:19 评论数:0 点赞数:0 收藏数:0

一、把页面拆分组件
可以拆分为4个组件,组件文件的后缀名可以是js,也可以是jsx格式。
入口文件 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/app/app';

ReactDOM.render(<App/>, document.getElementById('root'))
应用组件app.js
import React from 'react'
import CommentAdd from '../comment-add/comment-add'
import CommentList from '../comment-list/comment-list'
class App extends React.Component {
constructor (props) {

super(props)
this.state = {
comments: []
}
function(){ //外汇代理 http://www.kaifx.cn/ib/
this.delete = this.delete.bind(this)

}

componentDidMount () {

//模拟异步获取数据
setTimeout(() => {
const comments = [
{
username: "Tom",
content: "ReactJS好难啊!",
id: Date.now()
},
{
username: "JACK",
content: "ReactJS还不错!",
id: Date.now() + 1
}
]
this.setState({
comments:comments
})
}, 1000)

}

//用箭头函数就不需要bind绑定组件的this
add = (comment) => {

let comments = this.state.comments
comments.unshift(comment)
this.setState({ comments })

}

delete (index) {

let comments = this.state.comments
comments.splice(index, 1)
this.setState({ comments })

}

render () {

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 add={this.add}/>
<CommentList comments={this.state.comments} delete={this.delete}/>
</div>
</div>
)

}
}

export default App
添加评论表单组件comment-add.jsx
import React from 'react'
import PropTypes from 'prop-types'

class CommentAdd extends React.Component {
constructor (props) {

super(props)
this.state = {
username: '',
content: ''
}
this.addComment = this.addComment.bind(this)
this.changeUsername = this.changeUsername.bind(this)
this.changeContent = this.changeContent.bind(this)

}

addComment () {

// 根据输入的数据创建评论对象
let { username, content } = this.state
let comment = { username, content }
// 添加到comments中, 更新state
this.props.add(comment)
// 清除输入的数据
this.setState({
username: '',
content: ''
})

}

changeUsername (event) {

this.setState({
username: event.target.value
})

}

changeContent (event) {

this.setState({
content: event.target.value
})

}
render () {

return (
<div className="col-md-4">
<form className="form-horizontal">
<div className="form-group">
<label>用户名</label>
<input type="text" className="form-control" placeholder="用户名"
value={this.state.username} onChange={this.changeUsername}/>
</div>
<div className="form-group">
<label>评论内容</label>
<textarea className="form-control" rows="6" placeholder="评论内容"
value={this.state.content} onChange={this.changeContent}></textarea>
</div>
<div className="form-group">
<div className="col-sm-offset-2 col-sm-10">
<button type="button" className="btn btn-default pull-right" onClick={this.addComment}>提交</button>
</div>
</div>
</form>
</div>
)

}
}
CommentAdd.propTypes = {
add: PropTypes.func.isRequired
}

export default CommentAdd
评论列表组件comment-list.jsx
import React from 'react'
import PropTypes from 'prop-types'
import CommentItem from '../comment-item/comment-item'
import './commentList.css'

class CommentList extends React.Component {
constructor (props) {

super(props)

}

render () {

let comments = this.props.comments
let display = comments.length > 0 ? 'none' : 'block'
return (
<div className="col-md-8">
<h3 className="reply">评论回复:</h3>
<h2 style={{ display: display }}>暂无评论,点击左侧添加评论!!!</h2>
<ul className="list-group">
{
comments.map((comment, index) => {
console.log(comment)
return <CommentItem comment={comment} key={index} index={index} delete={this.props.delete}/>
})
}
</ul>
</div>
)

}
}
CommentList.propTypes = {
comments: PropTypes.array.isRequired,
delete: PropTypes.func.isRequired
}

export default CommentList
评论列表的item组件comment-item.jsx
import React from 'react'
import PropTypes from 'prop-types'
import './commentItem.css'

class CommentItem extends React.Component {
constructor (props) {

super(props)
this.deleteComment = this.deleteComment.bind(this)

}

deleteComment () {

let username = this.props.comment.username
if (window.confirm(`确定删除${username}的评论吗?`)) {
this.props.delete(this.props.index)
}

}

render () {

let comment = this.props.comment
return (
<li className="list-group-item">
<div className="handle">
<a href="javascript:" onClick={this.deleteComment}>删除</a>
</div>
<p className="user"><span >{comment.username}</span><span>说:</span></p>
<p className="centence">{comment.content}</p>
</li>
)

}
}
CommentItem.propTypes = {
comment: PropTypes.object.isRequired,
index: PropTypes.number.isRequired,
delete: PropTypes.func.isRequired
}

export default CommentItem
如果没有prop-types 需要安装,一般react脚手架默认就安装了。
npm install --save prop-types

版权声明
本文为[zhuanzhudeyipi]所创,转载请带上原文链接,感谢
https://segmentfault.com/a/1190000021441594