1、把页面拆分组件
能够拆分为4个组件,组件文件的后缀名能够是js,也能够是jsx格式。
入口文件 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/app/app';javascript
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) {css
super(props) this.state = { comments: [] } function(){ //外汇代理 http://www.kaifx.cn/ib/ this.delete = this.delete.bind(this)
}java
componentDidMount () {react
//模拟异步获取数据 setTimeout(() => { const comments = [ { username: "Tom", content: "ReactJS好难啊!", id: Date.now() }, { username: "JACK", content: "ReactJS还不错!", id: Date.now() + 1 } ] this.setState({ comments:comments }) }, 1000)
}npm
//用箭头函数就不须要bind绑定组件的this
add = (comment) => {app
let comments = this.state.comments comments.unshift(comment) this.setState({ comments })
}dom
delete (index) {异步
let comments = this.state.comments comments.splice(index, 1) this.setState({ comments })
}函数
render () {ui
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