使用es6语法与原本es5语法几个有区别的地方

1.React.creatClass与React.Component

 var Component = React.createClass({
render() {
return (
<div></div>
);
}
});
 class Component extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div></div>
);
}
}

2.propType和defaultProps

 var Component = React.createClass({
propTypes: {
name: React.PropTypes.string
},
getDefaultProps() {
return { };
},
render() {
return (
<div></div>
);
}
});
 class Component extends React.Component {

   /* static propTypes = { // as static property
title: React.PropTypes.string //需要babel转码 };*/ constructor(props) {
super(props);
} render() {
return (
<h1>{this.props.title}</h1>
);
}
} //静态属性,注意这两个属性都是加到类本身的属性而不是类的实例的属性
Component.propTypes = {
title : React.PropTypes.string.isRequired //被要求是字符串类型,其他类型报错
}; Component.defaultProps = {
title : 'hello world'
};

3.状态区别

 var Component = React.createClass({
// return an object
getInitialState(){
return {
isEditing: false
}
}
render(){
return <div></div>
}
})
 class Component extends React.Component{
constructor(props){
super(props);
this.state = { // define this.state in constructor
isEditing: false
}
}
render(){
return <div></div>
}
}

4.this区别

 var Components = React.createClass({
handleClick() {
console.log(this); // React Component instance
},
render() {
return (
<div onClick={this.handleClick}></div>//会切换到正确的this上下文
);
}
});
 class Components extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);//需要手动处理一下this
}
handleClick() {
console.log(this); // React Component instance
}
render() {
return (
<div onClick={this.handleClick}></div>
);
}
}

5.ReactDOM.findDOMNode

 class Component extends React.Component {
componentDidMount() {
let child = ReactDOM.findDOMNode(this.child);  //如果ref绑定在组件中则需要调用ReactDOM.findDOMNode()方法来拿取DOM节点
let title = this.title;  //如果ref绑定在虚拟DOM中,则可以直接拿到DOM节点
}
render() {
return(
<div>
<ChildComponent ref={(div) => {this.child = div;}} /> //官方文档中建议用回调函数的形式来写ref属性
<h1 ref={(h1) => {this.title = h1;}}>...</h1>
</div>
);
}
}

新版react踩坑总结的更多相关文章

  1. 【React踩坑记一】React项目中禁用浏览器双击选中文字的功能

    常规项目,我们只需要给标签加一个onselectstart事件,return false就可以 例: <div onselectstart="return false;" & ...

  2. 【React踩坑记五】React项目中引入并使用react-ace代码编辑插件(自定义列表提示)

    最近有一个引入sql编辑器插件的需求,要求代码高亮显示,代码智能提示,以及支持自定义代码提示列表等功能.中途在自定义代码提示列表中由于没有相关demo,所以踩了一些坑,遂将其整理如下,以便日后查看. ...

  3. 航遇项目react踩坑

    1.iconfont应用: a.正常用法如下 <span className='iconfont' > iconfont的代码,例如: </span> b.react不能动态 ...

  4. React 踩坑记录

    1.React-router error: super expression must either be null or a function 原因:引入babel后写ES6风格的代码: class ...

  5. 【React踩坑记四】React项目中引入并使用js-xlsx上传插件(结合antdesign的上传组件)

    最近有一个前端上传并解析excel/csv表格数据的需求. 于是在github上找到一个14K star的前端解析插件 github传送门 官方也有,奈何实在太过于浅薄.于是做了以下整理,避免道友们少 ...

  6. 【React踩坑记三】React项目报错Can't perform a React state update on an unmounted component

    意思为:我们不能在组件销毁后设置state,防止出现内存泄漏的情况 分析出现问题的原因: 我这里在组件加载完成的钩子函数里调用了一个EventBus的异步方法,如果监听到异步方法,则会更新state中 ...

  7. 【React踩坑记二】react项目实现JS路由跳转

    这里使用的是4.31版本的react-router-dom "react-router-dom": "^4.3.1", 直接使用以下代码即可实现路由跳转 thi ...

  8. 【React踩坑记六】create-react-app创建的react项目通过iP地址访问(实现局域网内访问)

    同项目组的小伙伴想用自己的电脑访问我电脑上开发阶段的create-react-app创建的react项目. 试过了了各种内网穿透工具ngrok以及localtunnel等. 奈何打开效率实在太过于龟速 ...

  9. React踩坑笔记 —— React

    Webpack提供了自己的导入方式require.include,但同时也支持commonjs规范或AMD规范的require语法,而Node.js使用的就是common.js,ES6的语法Impor ...

随机推荐

  1. ORA-15025: could not open disk "/dev/asm***"--转载

    Symptoms: 打完补丁后,数据库报错ORA-15025,数据库无法启动. alert日志信息: Wed Jul22 16:26:57 2015 ORA-15025:could not open ...

  2. Shell中取得文件的最后修改时间

    stat -c %y file 取得修改日期 -,-,-

  3. C++设计模式-Proxy代理模式

    Proxy代理模式 作用:为其他对象提供一种代理以控制对这个对象的访问. 代理的种类: 如果按照使用目的来划分,代理有以下几种: 远程(Remote)代理:为一个位于不同的地址空间的对象提供一个局域代 ...

  4. 使用微软CORS包不能跨域访问的问题

    使用jquery的ajax异步调用的时候会出现不能跨域访问的问题,这个问题一般有两种方法. 1:使用jsonp跨域 2:使用html5的CORS 在这里只谈论第二种,微软对CORS提供的了支持,在Nu ...

  5. Maven(二)使用eclipse创建maven多模块项目

    maven作为一种自动化构建工具,在现在的企业应用开发中运用非常普遍. 企业项目一般都比较大,多采用maven管理的多模块项目,下面直接上创建步骤 一.创建一个maven项目

  6. 杭电acm 1003

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...

  7. JQ判断复选框是否选中

    方法一: if($('#checkbox').is(':checked')) {} 方法二:if ($('#checkbox').attr('checked')) {} 方法三:if ($(" ...

  8. DOM杂记

    INPUT 监听输入框值的变化:"input"

  9. C/C++程序员应聘试题剖析(转载)

    转载自:http://www.cnitblog.com/zouzheng/articles/21856.html 1.引言 本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面 ...

  10. 施耐德Sepam 40系列备自投逻辑

    1# 主供: VL1= NOT PVTS_1_3 V1 = VL1 AND P59_1_7 AND P59_1_8 AND P59_1_9VL2 = VL1 AND I12 AND I21 AND I ...