在使用ES6类时,“超级()”和“超级(道具)”有什么区别?什么时候通过?props到super()为什么?class MyComponent extends React.Component {
constructor(props) {
super(); // or super(props) ?
}}
3 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
propssuper():
this.props
class MyComponent extends React.Component {
constructor(props) {
super(props)
console.log(this.props)
// -> { icon: 'home', … }
}}class MyComponent extends React.Component {
constructor(props) {
super()
console.log(this.props)
// -> undefined
// Props parameter is still available
console.log(props)
// -> { icon: 'home', … }
}
render() {
// No difference outside constructor
console.log(this.props)
// -> { icon: 'home', … }
}}propssuperthis.propsconstructorrender, shouldComponentUpdate
类组件应该始终使用 props.
添加回答
举报
0/150
提交
取消
