Facebook的ReactJS库对which component methods can be overridden and how有严格的规定。除非它是specifically allowed,否则我们不能重新定义方法。
对于我的自定义混入,如果我有一个我想要重写的方法,我该如何更新SpecPolicy呢?这有可能吗?
这个例子有点做作,但它应该能让人明白这一点。假设我有下面的mixin,它试图提供一个默认的renderItem
方法,如果需要的话,它将被覆盖。当我试图呈现组件<Hello ... />
时,我得到了一个Invariant Violation
错误。你可以找到a jsfiddle here。
var MyMixin = {
render: function () {
return this.renderItem();
},
renderItem: function () {
return <div>Hello {this.props.name}</div>;
}
};
var Hello = React.createClass({
mixins: [MyMixin],
renderItem: function() {
return <div>Hey {this.props.name}</div>;
}
});
https://stackoverflow.com/questions/23984887
复制相似问题