慕田峪4524236
2019-07-05 18:37:07
在使用ES6類時(shí),“超級(jí)()”和“超級(jí)(道具)”有什么區(qū)別?什么時(shí)候通過(guò)?props到super()為什么?class MyComponent extends React.Component {
constructor(props) {
super(); // or super(props) ?
}}
3 回答

慕碼人8056858
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
props
super()
:
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', … } }}
props
super
this.props
constructor
render
, shouldComponentUpdate
類組件應(yīng)該始終使用 props
.
添加回答
舉報(bào)
0/150
提交
取消