【发布时间】:2019-03-30 22:18:59
【问题描述】:
在构造函数中访问 props 的正确方法是什么?是的,我知道在 React 文档中是这样说的
在实现 React.Component 子类的构造函数时,你 应该在任何其他语句之前调用 super(props)。否则, this.props 将在构造函数中未定义,这可能会导致错误
更清楚地说,如果我们可以在构造函数中使用道具,为什么我们需要this.props
class MyComponent extends React.Component {
constructor(props) {
super(props)
console.log(props)
// -> { something: 'something', … }
// absolutely same
console.log(this.props)
// -> { something: 'something', … }
}
}
在某些情况下使用props 而不是this.props ?
【问题讨论】:
标签: javascript reactjs class ecmascript-6 constructor