创建组件
ES6 class创建的组件语法更加简明,也更符合javascript。内部的方法不需要使用function关键字。
React.createClass1
2
3
4
5
6
7
8
9
10import React from 'react';
const MyComponent = React.createClass({
render: function() {
return (
<div>以前的方式创建的组件</div>
);
}
});
export default MyComponent;
React.Component(ES6)1
2
3
4
5
6
7
8
9
10import React,{ Component } from 'react';
class MyComponent extends Component {
render() {
return (
<div>ES6方式创建的组件</div>
);
}
}
export default MyComponent;
属性
props propTypes and getDefaultProps
. 使用React.Component创建组件,需要通过在constructor中调用super()将props传递给React.Component。另外react 0.13之后props必须是不可变的。
. 由于是用ES6 class语法创建组件,其内部只允许定义方法,而不能定义属性,class的属性只能定义在class之外。所以propTypes要写在组件外部。
. 对于之前的getDefaultProps方法,由于props不可变,所以现在被定义为一个属性,和propTypes一样,要定义在class外部。
React.createClass1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import React from 'react';
const MyComponent = React.createClass({
propTypes: {
nameProp: React.PropTypes.string
},
getDefaultProps() {
return {
nameProp: ''
};
},
render: function() {
return (
<div>以前的方式创建的组件</div>
);
}
});
export default MyComponent;
React.Component(ES6)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22import React,{ Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>ES6方式创建的组件</div>
);
}
}
MyComponent.propTypes = {
nameProp: React.PropTypes.string
};
MyComponent.defaultProps = {
nameProp: ''
};
export default MyComponent;
状态
. 使用ES6 class语法创建组件,初始化state的工作要在constructor中完成。不需要再调用getInitialState方法。这种语法更加的符合JavaScript语言习惯。
React.createClass1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import React from 'react';
const MyComponent = React.createClass({
getInitialState: function() {
return { data: [] };
},
render: function() {
return (
<div>以前的方式创建的组件</div>
);
}
});
export default MyComponent;
React.Component(ES6)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import React,{ Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = { data: [] };
}
render() {
return (
<div>ES6方式创建的组件</div>
);
}
}
export default MyComponent;
this
. 使用ES6 class语法创建组件, class中的方法不会自动将this绑定到实例中。必须使用 .bind(this)或者 箭头函数 =>来进行手动绑定。
React.createClass1
2
3
4
5
6
7
8
9
10
11
12
13
14import React from 'react';
const MyComponent = React.createClass({
handleClick: function() {
console.log(this);
},
render: function() {
return (
<div onClick={this.handleClick}>以前的方式创建的组件</div>
);
}
});
export default MyComponent;
React.Component(ES6)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import React,{ Component } from 'react';
class MyComponent extends Component {
handleClick() {
console.log(this);
}
render() {
return (
<div onClick={this.handleClick.bind(this)}>ES6方式创建的组件</div>
);
}
}
export default MyComponent;
也可以将绑定方法写到constructor中:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import React,{ Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log(this);
}
render() {
return (
<div onClick={this.handleClick}>ES6方式创建的组件</div>
);
}
}
export default MyComponent;
或者使用箭头函数 => :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import React,{ Component } from 'react';
class MyComponent extends Component {
handleClick = () => {
console.log(this);
}
render() {
return (
<div onClick={this.handleClick}>ES6方式创建的组件</div>
);
}
}
export default MyComponent;
Mixins
. 使用ES6语法来创建组件是不支持React mixins的,如果一定要使用React mixins就只能使用React.createClass方法来创建组件了。
1 | import React,{ Component } from 'react'; |
未完待续。。。