es6用在React中的写法总结:

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

1.引用外部文件

//es5写法
var React = require('react'); var {Image,Text}= React;
//es6
import React,{Image,Text} from 'react';

2.创建React模块

//es5
var MyComponent = React.createClass({ ... }); module.exports = MyComponent;
//es6:使用export default来实现

 export default class MyComponent extends React.Component{

              ...

}

3.创建React组件

//es5
var Input = React.createClass({
render:function(){
return (
<Input source={this.props.source} />
);
}
});
//在es6里,通过定义一个继承自React.Component的class来定义一个组件类
class Input extends React.Component{
render(){
return(
<Input source={this.props.source} />
);
} }

4.创建React函数方式

//es5
var Input = React.createClass({
getInitialState:function(){
...
},
render:function(){
renturn(
<Input />
);
}
});
//es6
class Input extends React.Component{
getInitialState(){
...
}
render(){
<Input />
}
}

5.初始化state

//es5
var Star = React.createClass({
getInitialState:function(){
return{
sayHello:this.props.hello
}
}
});
//es6 有2种写法
第一种:
class Star extends React.Component{
state = {
sayHello:this.props.hello
}
} 第二种:
class Star extends React.Component{
constructor(props){
super(props);
this.state = {
sayHello:this.props.hello
}
}
}

5.定义组件属性类型和默认属性

//es5
var Star = React.createClass({
getDefaultProps:function(){
return{
minVal:0,
maxVal:true
}
},
propTypes:{
minVal:React.PropTypes.number.isRequired,
maxVal:React.PropTypes.bool.isRequired
},
render:function(){
return (
<View />
)
}
});
//es6里可以统一使用static实现.在static成员之间用;号隔开
class Star extends React.Component{
static defaultProps = {
minVal:0,
maxVal:true
};//注意;号
static propType = {
minVal:React.PropTypes.number.isReqired,
maxVal:React.PropTypes.bool.isRequired
};
render(){
return (
<View />
)
}
} //另一种写法
class Star extends React.Component{
render(){
return(
<View />
)
}
} Star.defaultProps = {
minVal:0,
maxVal:true
} Star.propTypes = {
minVal:React.PropTypes.number.isRequired,
maxVal:React.PropTypes.bool.isRequired
}

 

React,React Native中的es5和es6写法对照的更多相关文章

  1. 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-tabl ...

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

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

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

      ES5 ES6 模块 var React = require("react-native); var { Image, Text, View } = React;   import Re ...

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

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

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

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

  6. 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- ...

  7. React入门 (1)—使用指南(包括ES5和ES6对比)

    前言 本篇会简明扼要的介绍一下React的使用方法.代码会用JSX+ES5和JSX+ES6两种方式实现. React简介 React来自Facebook,于2013年开源.至今不断修改完善,现在已经到 ...

  8. 在 React Native 中使用 moment.js 無法載入語系檔案

    moment.js 是很常見的日期時間 library,友善的 API 與極佳的執行效率是它的兩大賣點.例如 (new Date()).getFullYear(),如果使用 moment.js 我可以 ...

  9. 在 React Native 中使用 Redux 架构

    前言 Redux 架构是 Flux 架构的一个变形,相对于 Flux,Redux 的复杂性相对较低,而且最为巧妙的是 React 应用可以看成由一个根组件连接着许多大大小小的组件的应用,Redux 也 ...

随机推荐

  1. APUE学习--第三版apue编译

    第三版apue编译:     1. 首先在  http://www.apuebook.com/   下载源码解压:      tar zxvf src.3e.tar.gz 看完Readme可知,直接执 ...

  2. Oracle的动态性能视图[持续更新]

    前言 .... v$version:查看数据库版本 其中 Oracle Database:指代数据库版本 PL/SQL:ORACLE对于标准SQL的超集,全称Procedural Language/S ...

  3. hosts 屏蔽百度

    127.0.0.1 localhost  cpro.baidu.com vie.baidu.com  cpro.baidu.com  ubmcmm.baidustatic.com  uumcmm.ba ...

  4. 360随身wifi在win10中连不上网络

    找到服务"Wired AutoConfig"和"WLAN AutoConfig"项,点击"启动"按钮,确保使其正常启动. 讲本地网卡共享到移 ...

  5. C语言中的system函数参数及其作用

    函数名: system 功   能: 发出一个DOS命令  用   法: int system(char *command);  system函数已经被收录在标准c库中,可以直接调用 system() ...

  6. NXP NFC移植及学习笔记(原创)

    NFC功能介绍 NFC 目前使用的三种功能: 1. P2P模式:基于LLCP协议的基础上,以NDEF数据交换格式来通信. 2. 读写模式:当作为读卡器,对NFC Tag的读写. 3. 卡模拟模式:模块 ...

  7. Bing Test -必应每日壁纸自动换

    今天向大家推荐一个桌面美化类的工具,没错就是自动更换壁纸,而且是精美的必应每日壁纸哦!绿色小巧,开机自启动,设置后每日自动更新你的桌面~ 软件名称:Bing Test 链接: http://pan.b ...

  8. Best code水题之路

    BestCoder 2nd Anniversary: 1001.Oracle There is once a king and queen, rulers of an unnamed city, wh ...

  9. 我的CodeF水A题之路

    Codeforces Round #359 (Div. 2) A. Free Ice Cream 题目链接:http://www.codeforces.com/problemset/problem/6 ...

  10. c#_图表之zeGraph

    关于图表控件: http://blog.csdn.net/sgear/article/details/1449025 其中zedGraph的基本方法及属性介绍: http://wenku.baidu. ...