React的ES5与ES6写法对比

ES6有许多新的特性,语法也在ES5上有较大的变化,在React中应用广泛,下面列举部分示例,体会一下ES6的语法给我们带来的便利…

http://coderlt.coding.me/2016/05/17/React-ES5-ES6/

导入模块

ES5

      
      
var React = require( 'react');
var PropTypes = React.PropTypes;

ES6

使用 import 导出模块或模块中的方法

      
      
import React, {PropTypes} from 'react'

导出模块

ES5

      
      
var Header = React.createClass({ ... })
module.exports = Header;

ES6

使用 export default 来导出模块中的内容

      
      
export default class Header extends Component{ ... }

组件定义

ES5

      
      
var Photo = React.createClass({
render: function(){}
})

ES6

使用 class 关键字标识为组件 使用 extends 来继承父组件

      
      
class Photo extends React.Component {
render(){
return(
< Image source={ this.props.source} />
)
}
}

添加组件的事件

ES5

事件绑定不需要绑定this

      
      
var Photo = React.createClass({
clickEv: function(e){ ... },
render: function(){
return (
<div onClick={this.clickEv}></div>
)
}
})

ES6

使用 HTML 中的事件绑定属性

事件绑定需要绑定this

      
      
class Photo extends React.Component{
clickEv(){ ... }
render(){
return (
<div onClick={ this.clickEv.bind( this)}></div>
)
}
}

指定组件的属性类型和默认属性

ES5

      
      
var Video = React.createClass({
getDefaultProps: function(){
return {
autoPlay: false,
maxLoops: 10
}
},
propTypes: {
autoPlay: React .PropTypes .bool .isRequired,
maxLoops: React .PropTypes .number .isRequired,
posterFrameSrc: React .PropTypes .string .isRequired,
videoSrc: React .PropTypes .string .isRequired,
},
render: function(){
return (
<View />
)
}
})

ES6

使用 static 关键字标识为组件的属性

      
      
class Video extends React.Component{
static defaultProps = {
autoPlay: false,
maxLoops: 10
}
static propTypes = {
autoPlay: React .PropTypes .bool .isRequired,
maxLoops: React .PropTypes .number .isRequired,
posterFrameSrc: React .PropTypes .string .isRequired,
videoSrc: React .PropTypes .string .isRequired,
}
render(){
return (
<View />
)
}
}

STATE 初始化

ES5

      
      
var Video = React.createClass({
getInitialState: function(){
return (
loopsRemaining: this.props.maxLoops
)
}
})

ES6

在构造函数 constructor 中使用 this.state 来指定组件的状态

      
      
class Video extends React.Component {
constructor(props){
super(props)
this.state = {
loopsRemaining: this.props.maxLoops
}
}
}

参数传递

ES5

      
      
var PostInfo = React.createClass({
getInitialState: function(){
return (
showModal: false,
)
},
btnClickEv: function(e){
this.setState({ showModal: true });
},
render: function(){
return (
<div onClick={this.btnClickEv}>{this.props.label}</div>
)
}
})

ES6

使用箭头函数传递参数

      
      
class PostInfo extends React.Component{
btnClickEv(e){
this.setState({ showModal: true });
}
render(){
<div>
<div onClick={e=> this.btnClickEv(e)}>{ this.props.label}</div>
<div onClick={ this.btnClickEv.bind( this)}>{ this.props.label}</div>
</div>
}
}

对象解构 与 属性延展

      
      
class AutoloadingPostsGrid extends React.Component {
render() {
var {
className,
...others, // contains all properties of this.props except for className
} = this.props;
return (
<div className={className}>
< PostsGrid {...others} />
<button onClick={ this.handleLoadMoreClick}> Load more</button>
</div>
);
}
}

let const

ES5

      
      
for (var i = 0; i< arr. length; i++){}
console. log( i)

ES6

      
      
for( let i = 0; i < arr.length; i++){}
console. log(i);
const PI = 3.1415;
PI = 3;
// TypeError: Assignment to constant variable.

promise

      
      
var promise = new Promise( function(resolve, reject){
var image = new Image();
image.onload = function() {
resolve(image);
};
image.onerror = function() {
reject( new Error( 'Could not load image at ' + url));
};
image.src = url;
})
promise.then(resoveCallback, rejectCallback);
promise.then( function(){}, function(){});
promise.then( function(){}).catch( function(){});
Promise.all([p1, p2, p3]).then().catch()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值