React.createClass
- 创建组件类的方法
通过 extends 更好地与 ES6 module一起使用React.Component
,它扩展了 Component 类而不是调用createClass
两者之间的差异很小,
语法:
React.createClass:
var MyComponent = React.createClass({ });
react.组件:
export default class MyComponent extends React.Component{ }
getInitialState() :
React.createClass: 是 React.Component: 否
构造函数():
React.createClass:否 React.Component:是
propTypes 语法:
React.createClass:
var MyComponent = React.createClass({
propTypes: { }
});
react.组件:
export default class MyComponent extends React.Component{ }
MyComponent.prototypes = { }
默认属性:
React.createClass:
var MyComponent = React.createClass({
getDefaultProps(): { return {} }
});
react.组件:
export default class MyComponent extends React.Component{ }
MyComponent.defaultProps = { }
状态:
React.createClass:
State changes can be made inside getInitialState() function
react.组件:
State changes can be made inside constructor(props) function
这个:
React.createClass:
automatically bind 'this' values.
Ex: <div onClick={this.handleClick}></div>
'this' can be accessed by default in handleClick function
react.组件:
whereas here we need to bind explicitly,
Ex: <div onClick={this.handleClick.bind(this)}></div>