根據標題,爲什麼最初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) –