转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-es5-and-es6-writing-table/

对于很多初次学习React-Native人来说,都会为ES6语法所困惑,因为网上好多React-Native的Demo都是用ES5语法写的。所以我刚开始也是学的ES5,对我来说ES5语法比较熟悉,但是看到ES6的语法刚开始就感觉怪怪的,相信对初次接触ES6写法的

人来说都会有这样的感觉。今天我就整理下ES5跟ES6写法的规范,希望会对你有所帮助。

一、模块引用

在ES5里引入React的包基本通过require进行,代码如下:

//ES5

var React=require('react-native');
var {
Image,
Text,
propTypes
}=React;

二、导出单个类

//在ES6中用,import导入

import React,{Image,Text,PropTypes} from 'react-native'

在ES5中,一般通过module.exports来导出

//ES5

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

//ES6

export default class MyComponent extends React.component{
....
}

引用的时候:

//ES5

var MyComponent=require('./MyComponent.js');
 

//ES6

import MyComponent from './MyComponent.js'

三、定义组件

在ES5中,通过React.createClass来定义一个组件类。如下所示:

//ES5

var MyComponent=React.createClass({
render:function(){
return (
<Text>...</Text>
);
}
})

在ES6里,通过定义一个继承自React.Component的class来定义一个组件:

//ES6

class MyComponent extends React.component{
render(){
return(
<Text>...</Text>
)
}
}

四、给组件定义方法

从上面可以看出给组件定义方法不再用 函数名:function()的写法,而是直接名字,方法的后面也不用用逗号(,)

五、定义组件的属性类型和默认属性

在ES5里,属性类型和默认属性分别通过propTypes成员和getDefaultProps方法来实现

 //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成员来实现
//ES6
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如下:

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

ES6如下:

//ES6
class Video extends React.Component {
state = {
loopsRemaining: this.props.maxLoops,
}
} 不过我们推荐更易理解的在构造函数中初始化(这样你还可以根据需要做一些计算):
//ES6
class Video extends React.Component {
constructor(props){
super(props);
this.state = {
loopsRemaining: this.props.maxLoops,
};
}
}

七、把方法作为回调提供

//ES5

var MyComponent=React.createClass({
_example:function(){
console.log('example')
},
render:function(){
return(
<View>
<TouchableHighlight onPress={this._example}>
<Text>...</Text>
</TouchableHighlight>
</View>
)
}
})

//在ES6下可以通过bind来绑定引用,或者使用箭头函数(它会绑定当前的scope的this引用)来调用

class MyComponent extends React.component{
_example(){
console.log('example')
}
render(){
return(
<View>
<TouchableHighlight onPress={this._example.bind(this) or ()=>{this._example()}}> <Text>...</Text> </TouchableHighlight>
</View>
)
}
}

对于很多初次学习React-Native人来说,都会为ES6语法所困惑,因为网上好多React-Native的Demo都是用ES5语法写的。所以我刚开始也是学的ES5,对我来说ES5语法比较熟悉,但是看到ES6的语法刚开始就感觉怪怪的,相信对初次接触ES6写法的

人来说都会有这样的感觉。今天我就整理下ES5跟ES6写法的规范,希望会对你有所帮助。

一、模块引用

在ES5里引入React的包基本通过require进行,代码如下:

//ES5

var React=require('react-native');
var {
Image,
Text,
propTypes
}=React;

二、导出单个类

//在ES6中用,import导入

import React,{Image,Text,PropTypes} from 'react-native'

在ES5中,一般通过module.exports来导出

//ES5

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

//ES6

export default class MyComponent extends React.component{
....
}

引用的时候:

//ES5

var MyComponent=require('./MyComponent.js');
 

//ES6

import MyComponent from './MyComponent.js'

三、定义组件

在ES5中,通过React.createClass来定义一个组件类。如下所示:

//ES5

var MyComponent=React.createClass({
render:function(){
return (
<Text>...</Text>
);
}
})

在ES6里,通过定义一个继承自React.Component的class来定义一个组件:

//ES6

class MyComponent extends React.component{
render(){
return(
<Text>...</Text>
)
}
}

四、给组件定义方法

从上面可以看出给组件定义方法不再用 函数名:function()的写法,而是直接名字,方法的后面也不用用逗号(,)

五、定义组件的属性类型和默认属性

在ES5里,属性类型和默认属性分别通过propTypes成员和getDefaultProps方法来实现

 //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成员来实现
//ES6
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如下:

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

ES6如下:

//ES6
class Video extends React.Component {
state = {
loopsRemaining: this.props.maxLoops,
}
} 不过我们推荐更易理解的在构造函数中初始化(这样你还可以根据需要做一些计算):
//ES6
class Video extends React.Component {
constructor(props){
super(props);
this.state = {
loopsRemaining: this.props.maxLoops,
};
}
}

七、把方法作为回调提供

//ES5

var MyComponent=React.createClass({
_example:function(){
console.log('example')
},
render:function(){
return(
<View>
<TouchableHighlight onPress={this._example}>
<Text>...</Text>
</TouchableHighlight>
</View>
)
}
})

//在ES6下可以通过bind来绑定引用,或者使用箭头函数(它会绑定当前的scope的this引用)来调用

class MyComponent extends React.component{
_example(){
console.log('example')
}
render(){
return(
<View>
<TouchableHighlight onPress={this._example.bind(this) or ()=>{this._example()}}> <Text>...</Text> </TouchableHighlight>
</View>
)
}
}

react-native ES5与ES6写法对照表的更多相关文章

  1. React/React Native 的ES5 ES6写法对照表-b

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

  2. React Native 的ES5 ES6写法对照表

    模块 引用 在ES5里,如果使用CommonJS标准,引入React包基本通过require进行,代码类似这样: //ES5 var React = require("react" ...

  3. React/React Native 的ES5 ES6写法对照表

    //es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...

  4. React,React Native中的es5和es6写法对照

    es6用在React中的写法总结: 在es6还没有完全支持到浏览器的阶段里,已经有很多技术人员开始用es6的写法来超前编程了,因为有转义es6语法的工具帮助下,大家才可大量使用.解析看看es6写法用在 ...

  5. React Native中常用ES6语法

    一:模块导入导出 //ES6 import React, { Component, PropTypes, } from 'react'; import { Image, Text } from 're ...

  6. 【转】React Native中ES5 ES6写法对照

    很多React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教程和例子都是 ...

  7. React Native的语法之ES5和ES6

    原文地址:http://www.devio.org/2016/08/11/React-Native%E4%B9%8BReact%E9%80%9F%E5%AD%A6%E6%95%99%E7%A8%8B- ...

  8. React Native学习笔记之2

    1:如何创建一个react native工程 首先进入到指定文件夹里面,然后在终端执行react-native init ReactNativeProject :其中ReactNativeProjec ...

  9. iOS 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

随机推荐

  1. Eclipse与github整合

    Eclipse与github整合 Windows系统下: github官方指南:https://help.github.com/articles/set-up-git Git?是个正快速成长的版本控制 ...

  2. 判断Javascript变量是否为空 undefined 或者null(附样例)

    1.变量申明未赋值 var type; //type 变量未赋值 1. type==undefined //true 2. type===undefined //true 3. typeof(type ...

  3. 随机生成三个数(break用法)

  4. hdu4533 线段树维护分段函数

    更新:x1,y1,x2,y2不用long long 会wa.. #include<iostream> #include<cstring> #include<cstdio& ...

  5. python 全栈开发,Day73(django多表添加,基于对象的跨表查询)

    昨日内容回顾 多表方案: 如何确定表关系呢? 表关系是在2张表之间建立的,没有超过2个表的情况. 那么相互之间有2条关系线,先来判断一对多的关系. 如果其中一张表的记录能够对应另外一张表的多条记录,那 ...

  6. ERP商品管理业务逻辑封装(三十四)

    产品购进管理业务逻辑: public class ProductBLL { /// <summary> /// 产品对象添加 并且返回产品编号 /// </summary> / ...

  7. hdu 1711( 模式串T在主串S中首次出现的位置)

    Sample Input213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1 Sample Out ...

  8. la 4394

    题解: 区间dp 令f[i][j]表示搞好i-j的最小值 首先如果不用涂色 那么可以从f[i][k] f[k+1][j]转移 如果要涂色,那么就从f[i][k][a](表示i-k全为a)+f[k+1] ...

  9. C#: 执行批处理文件(*.bat)的方法

    static void Main(string[] args) { Process proc = null; try { proc = new Process(); proc.StartInfo.Fi ...

  10. HDU Tody HDU2112

    不想用floyd了 也不一定适合  floyd只能处理小数据 dijkstra算法 wa了很久   一个是dijkstra里面的u   导致RE了无数次   下标溢出 还有就是注意细节  当起点和终点 ...