react-native ES5与ES6写法对照表
转载链接: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写法对照表的更多相关文章
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
- React Native 的ES5 ES6写法对照表
模块 引用 在ES5里,如果使用CommonJS标准,引入React包基本通过require进行,代码类似这样: //ES5 var React = require("react" ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- React,React Native中的es5和es6写法对照
es6用在React中的写法总结: 在es6还没有完全支持到浏览器的阶段里,已经有很多技术人员开始用es6的写法来超前编程了,因为有转义es6语法的工具帮助下,大家才可大量使用.解析看看es6写法用在 ...
- React Native中常用ES6语法
一:模块导入导出 //ES6 import React, { Component, PropTypes, } from 'react'; import { Image, Text } from 're ...
- 【转】React Native中ES5 ES6写法对照
很多React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教程和例子都是 ...
- 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- ...
- React Native学习笔记之2
1:如何创建一个react native工程 首先进入到指定文件夹里面,然后在终端执行react-native init ReactNativeProject :其中ReactNativeProjec ...
- iOS 写给iOS开发者的React Native学习路线(转)
我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...
随机推荐
- 反向代理负载均衡之APACHE
反向代理负载均衡之APACHE 一.反向代理1.1 介绍反响代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将 ...
- 最全Kafka 设计与原理详解【2017.9全新】
一.Kafka简介 1.1 背景历史 当今社会各种应用系统诸如商业.社交.搜索.浏览等像信息工厂一样不断的生产出各种信息,在大数据时代,我们面临如下几个挑战: 如何收集这些巨大的信息 如何分析它 如何 ...
- Object Detection
这篇博客对目标检测做了总结:https://handong1587.github.io/deep_learning/2015/10/09/object-detection.html
- ssh-keygen -t rsa -b 4096 -C "邮箱"
ssh-keygen -t rsa -b 4096 -C "邮箱":这条命令的目的是为了让本地机器ssh登录远程机器上的GitHub账户无需输入密码.将这条命令分解: 1.ssh- ...
- Java 变量、循环、判断
粗糙笔记不喜勿喷 Java 8大基本类型 第一类:逻辑型(boolean) 1.boolean类型只存在true(真),false(假)两种形式 例: boolean a=true; boolean ...
- locust的安装与使用
Contents Locust这一款开源性能测试工具.然而,当前在网络上针对Locust的教程极少,不管是中文还是英文,基本都是介绍安装方法和简单的测试案例演示,但对于较复杂测试场景的案例演示却基本没 ...
- JavaScript中的短路
短路:逻辑运算从左到右.逻辑或运算,当左边的条件成立时,后面的条件将不再参与运算. 因此在逻辑或运算中,尽量将条件结果为true的放第一位.而在逻辑与运算中,尽量将条件结果为false的放到第一位. ...
- BBC 记录片planet earth
He'll have to remain on guard for another two weeks, but in the jungle, just surviving the day can c ...
- Android的简单应用(二)——使用dispatchKeyEvent双击退出程序
原文:https://www.cnblogs.com/cpacm/archive/2014/11/10/4087070.html Android系统按键操作最先是在dispatchKeyEvent ...
- POJ 1065 Wooden Sticks【贪心】
题意: 有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列.问至少要分为多少个序列.且要保证排出来的子序列数最少. 思路: ( 9 , 4 ) ,( 2 , 5 ) ,( 1 ...