根据标题,为什么最初this.props失败?更实际的是,如果你依赖构造函数中的道具,你如何解决这个问题?例如,我想在我的订阅中引用道具?为什么我无法在反应构造函数中访问默认道具,但我可以渲染?
class AboutBox extends Component {
static defaultProps = {
title: 'Undefined Product',
}
constructor() {
super();
console.log(this.props.title); //this fails (=> null)
}
render() {
console.log(this.props.title); //this works (=> 'Undefined Product')
return null;
}
}
相关:http://stackoverflow.com/q/30571875/2088135 –
可能重复[使用es6类时React中“super()”和“super(props)”之间的区别是什么?](http: //www.stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-in-react-when-using-e) –