由于我将代码重构为ES6,所以我将所有默认值移动到SomeClass.defaultProps = { ... }
。
假设存在类层次结构的情况,而我需要保留对整个层次结构的一些默认值。但是问题是defaultProps
不适用于扩展的类:
class AbstractComponent extends React.Component {
constructor(props) { super(props) }
}
class OneOfImplementations extends AbstractComponent {
constructor(props) { super(props) }
}
//Problem: hierarchy defaults doesn't work
AbstractComponent.defaultProps = { name: 'Super' }
小提琴例子
P.S.S.--我想知道,在整个层次结构中,保留共用(变量/函数)的最佳位置在哪里?也许在AbstractComponent
做这样的事
constructor(props) {
super(_.assign(props, {
commonValue: 128,
commonCallback: _.noop
}));
}
但问题是不可能覆盖子类中的一个属性
发布于 2015-10-30 10:40:33
"defaultProps“属性的声明顺序似乎很重要:
class AbstractComponent extends React.Component {
constructor(props) { super(props) }
render() {
return <div>Prop: [ {this.props.name} ]</div>
}
}
AbstractComponent.defaultProps = { name: 'Super' }
class ComponentImpl1 extends AbstractComponent {
constructor(props) { super(props) }
}
// works
http://jsfiddle.net/jwm6k66c/103/
https://stackoverflow.com/questions/33433153
复制相似问题