You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What's the difference between “super()” and “super(props)” in React when using es6 classes?
classMyComponentextendsReact.Component{constructor(props){super();// or super(props) ?}}
Answer
There is only one reason when one needs to pass props to super(): When you want to access this.props in constructor.
(Which is probably redundant since you already have a reference to it.)
This is explicitly said in one Ben Alpert's answer to similar question.
时间:2016-11-18 15:51:49
类别:React
Answer
本地测试
情况1: 不写 super(props)
结果
情况2: 写 super(props)
和答案说的一样,如果想在 constructor 里面使用 this.props.xxx 就需要写, 不然可以不写
参看文章
The text was updated successfully, but these errors were encountered: