是否有人被弃用,我应该使用较新的一个?
我应该创建组件还是React类来开发我的UI?
我看到一些使用Component的例子.例如:
export default class ItemList extends Component { constructor(props) { //binding functions this.renderHeader = this.renderHeader.bind(this); this.renderRow = this.renderRow.bind(this); super(props); this.state = { dataSource: this.props.state.planDataSource,planName: null } }
和其他人一起使用
var ItemList = React.createClass({ getInitialState: function() { return { dataSource: this.props.state.planDataSource,planName: null }; },
我正在学习反应原生的例子,我很困惑哪个是首选.
我最近将一个React类转换为一个组件,并发现“this”指针不起作用,因为React类使用自动绑定,而Components需要显式绑定.
The most notable new feature is support for ES6 classes,which allows developers to have more flexibility when writing components. Our eventual goal is for ES6 classes to replace React.createClass completely,but until we have a replacement for current mixin use cases and support for class property initializers in the language,we don’t plan to deprecate React.createClass.
https://facebook.github.io/react/blog/2015/03/10/react-v0.13.html
No Autobinding
Methods follow the same semantics as regular ES6
classes,meaning that they don’t automatically bind this to the
instance. You’ll have to explicitly use .bind(this) or arrow functions
=>.No Mixins
Unfortunately ES6 launched without any mixin support.
Therefore,there is no support for mixins when you use React with ES6
classes. Instead,we’re working on making it easier to support such
use cases without resorting to mixins.
https://facebook.github.io/react/docs/reusable-components.html