class App extends React.Component {
constructor(props){
// 必须要传递参数
super(props)
this.state = {
text: this.props.text
}
}
render() {
return (
// render不用传props
{this.props.children}
)
}
}
constructor和render内部this都指向组件实例,只要constructor内部要读取props就要写明这个参数,但是render不用,为什么呢?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
es6:
When you want to access this.props in constructor.
(Which is probably redundant since you already have a reference to it.)
所以只有在构造器
constructor内使用this.props的时候才写super(props), 不使用传入props也没错.
es5: