新搭建的个人博客,本文地点:React进修笔记3:用es2015(ES6)重写CommentBox
在一开始的时刻webpack设置中我们就加入了es2015的支撑,就是下面的设置,但之前的进修笔记都运用的es5完成,所以特地作一篇笔记,纪录运用es6完成CommentBox
query: {
presets: ['es2015','react']
}
1、建立相干文件
touch src/commentEs6.js
//修正webpack,增添该进口文件
entry:{
'index':'./src/index.js',
'comment':'./src/comment.js',
'commentEs6':'./src/commentEs6.js',
},
//修正build/index.html,引入该文件
<script src="commentEs6.js"></script>
2、做个简朴的测试,看下浏览器
//require悉数用import来完成
import React from 'react';
import ReactDom from 'react-dom';
//es6支撑类,经由过程类继续完成组件的开辟
class CommentBox extends React.Component {
render () {
return <div>Hello World</div>
}
}
//衬着
ReactDom.render(
<CommentBox />,document.getElementById('content')
)
3、直接贴出完全修正后的代码
import React from 'react';
import ReactDom from 'react-dom';
import marked from 'marked'
import $ from 'jquery'
class CommentBox extends React.Component {
constructor(props){
super(props)
//state 初始化直接在组织函数完成
this.state = {data:[]}
}
//函数的誊写体式格局也发生了变化
loadCommentsFromServer(){
//还不太邃晓,总之this到了ajax中指向的对象就发生了变化
var url = this.props.url
$.ajax({
url:url,
dataType:"json",
cache:false,
//应用箭头函数传参,能够少去了bind this
success:(data) => {
this.setState({data: data})
},
error:(xhr,status,err) => {
console.log(url,status,err.toString())
}
})
}
handleSubmitComment(data){
var url = this.props.url
$.ajax({
url:url,
type:"POST",
data:data,
dataType:"json",
cache:false,
success:(data) => {
this.setState({data:this.state.data.concat(data)});
},
error:(xhr,status,err) => {
console.log(this.props.url,status,err.toString())
}
})
}
componentDidMount(){
this.loadCommentsFromServer()
//setTimeout(this.loadCommentsFromServer,2000)
}
render() {
return (
<div className='commentBox'>
<h1>Comments</h1>
<CommentList data={this.state.data} />
<CommentForm onSubmitComment={this.handleSubmitComment.bind(this)} />
</div>
)
}
}
class CommentList extends React.Component{
constructor(props) {
super(props)
}
render(){
var commentNodes = this.props.data.map(function(comment){
return (
<Comment author={comment.author} key={comment.id}>
{comment.text}
</Comment>
)
});
return (
<div className='commentList'>
{commentNodes}
</div>
)
}
}
class Comment extends React.Component {
constructor(props) {
super(props)
}
rawMarkup(){
var rawMarkup = marked(this.props.children.toString(),{sanitize:true})
return {__html:rawMarkup}
}
render(){
return (
<div className='comment'>
<h2 className='commentAuthor'>
{this.props.author}
</h2>
<span dangerouslySetInnerHTML={this.rawMarkup()} />
</div>
)
}
}
class CommentForm extends React.Component{
constructor(props) {
super(props)
this.state = {author:"",text:""}
}
handleAuthorChange(event){
this.setState({author:event.target.value})
}
handleTextChange(event){
this.setState({text:event.target.value})
}
handleSubmit(event){
event.preventDefault();
var author = this.state.author.trim()
var text = this.state.text.trim()
if(!text||!author) {
return;
}
this.props.onSubmitComment({author:author,text:text});
this.setState({author:"",text:""})
return false;
}
render(){
return (
<form className='commentForm' onSubmit={this.handleSubmit.bind(this)}>
<input type='text' onChange={this.handleAuthorChange.bind(this)}
placeholder='怎样称谓您呢?爷' value={this.state.author} />
<input type='text' onChange={this.handleTextChange.bind(this)}
placeholder='爷有什么见教?' value={this.state.text} />
<input type='submit' value='提交' />
</form>
)
}
}
ReactDom.render(
<CommentBox url="http://localhost:8080/api/comment" />,document.getElementById('content')
)