React ES6与ES5写法的区别
来源:互联网 发布:淘宝在哪里交保证金 编辑:程序博客网 时间:2024/05/16 05:55
之前一直用的.net开发,react的写法也一直用的ES5的写法,觉得越来越out了,正好在学node,很多都用ES6的写法,就开始总结了一些React的ES6的写法。主要有以下几个方面:
一、组件的创建
ES5(React.createClass)
import React from 'react';const SkillComponent = React.createClass({ render:function(){ return( <div> <h2>我是技能</h2> </div> ) }})export default SkillComponent
ES6(React.Component)
import React, { Component } from 'react';class SkillComponent extends Component{ render(){ return( <div> <h2>我是技能</h2> </div> ) }}export default SkillComponent
二、组件内定义方法
ES5中组件定义方法是
componentDidMount:function(){ },注意,方法结束后必须写','
ES6中组件定义的方法是
componentDidMount(){ };注意,方法后的';'可写可不写;
三、设置默认属性
ES5中默认属性通过 getDefaultProps定义在组件的内部
import React from 'react';const SkillComponent = React.createClass({ getDefaultProps: function () { return { defaultString: '', defaultArr:[], defaultObject:{}, defaultBool:true }; }, componentDidMount:function(){ }, render:function(){ return( <div> <h2>我是技能</h2> </div> ) }})export default SkillComponent
ES6中需要通过在constructor构造函数中调用super将props传递给组件 并定义在组件的外部
import React, { Component } from 'react';class SkillComponent extends Component{ constructor(props) { super(props); }; componentDidMount(){ }; render(){ return( <div> <h2>我是技能</h2> </div> ) }}SkillComponent.propTypes = { defaultString: React.PropTypes.string.isRequired, defaultArr: React.PropTypes.array.isRequired, defaultObject : React.PropTypes.object.isRequired, defaultBool:React.PropTypes.bool.isRequired};SkillComponent.defaultProps = { defaultString: '', defaultArr:[], defaultObject:{}, defaultBool:true};export default SkillComponent
四、初始化状态(state)
ES5中初始化状态通过getInitialState函数定义
import React from 'react';const SkillComponent = React.createClass({ getInitialState:function(){ return{ dataSource:{} } }, render:function(){ return( <div> <h2>我是技能</h2> </div> ) }})export default SkillComponent
import React, { Component } from 'react';class SkillComponent extends Component{ constructor(props) { super(props); this.state = { dataSource: {} } }; render(){ return( <div> <h2>我是技能</h2> </div> ) }}export default SkillComponent
五、全局变量
ES5中全局变量定义在组件内
import React from 'react';const SkillComponent = React.createClass({ serialNumAll:null, render:function(){ return( <div> <h2>我是技能</h2> </div> ) }})export default SkillComponent
import React, { Component } from 'react';class SkillComponent extends Component{ constructor(props) { super(props); this.serialNumAll=null, }; componentDidMount(){ }; render(){ return( <div> <h2>我是技能</h2> </div> ) }}export default SkillComponent
六、this的指向
ES5中,React.createClass会把所有的方法都绑定(bind)一遍,在任意地方都可以作为回调函数
import React from 'react';const SkillComponent = React.createClass({ handleClick:function(){ console.log(this); }, render:function(){ return( <div> <h2>我是技能</h2> <input type="button" onclick={this.handleClick}/> </div> ) }})export default SkillComponent
import React, { Component } from 'react';class SkillComponent extends Component{ handleClick(){ console.log(this); } render(){ return( <div> <h2>我是技能</h2> <input type="button" onclick={this.handleClick.bind(this)}/> </div> ) }}export default SkillComponent
import React, { Component } from 'react';class SkillComponent extends Component{ handleClick = () => { console.log(this); } render(){ return( <div> <h2>我是技能</h2> <input type="button" onclick={this.handleClick.bind(this)}/> </div> ) }}export default SkillComponent
import React, { Component } from 'react';class SkillComponent extends Component{ constructor(props) { super(props); this.serialNumAll=null, this.state = { dataSource: {} }, this.handleClick = this.handleClick.bind(this); } handleClick(){ console.log(this); } render(){ return( <div> <h2>我是技能</h2> <input type="button" onclick={this.handleClick.bind(this)}/> </div> ) }}export default SkillComponent
ES5
findDataSource:function(bdate) { $.get(WebServiceBaseUrl + '/ProjectManage/ProjectAssessSource?bdate=' + bdate, function (result) { if (this.isMounted()) { this.setState({ dataSource: result }); } }.bind(this)); },ES6中废弃了isMounted()
findDataSource(busType, busStatus, company, buyDate, serviceLine) { $.get(WebServiceBaseUrl + '/ProjectManage/ProjectAssessSource?bdate=' + bdate, function (result){ this.setState({ dataSource: result }); }.bind(this)); };
阅读全文
0 0
- React ES6与ES5写法的区别
- React的ES5与ES6写法对比
- react的ES5与ES6写法的不同(一)
- React ES5与ES6语法写法比较
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- React/React Native 的ES5 ES6写法对照表
- BCC(Borland C++ Compiler)编译 ISAPI 扩展或者用MinGW也行
- 基础练习 闰年判断
- python pandas 查看数据的前五行
- The Elements of Computing Systems阅读笔记(5)
- s3c2440 ARM9 裸机驱动第三篇—定时器
- React ES6与ES5写法的区别
- SpringBoot依赖及其作用
- ubuntu修改系统/文件夹语言(中文/英文互转)
- 通过java反射机制操作对象的属性方法
- url编解码
- 2017年11月04日作业
- C++中产生1~100的随机数
- windows下安装linux双系统绕坑指南
- 关于写这个博客的原因(一些心路历程)