Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【20161118】difference between "super()" or "super(props)" in React using ES6 #51

Closed
zhongxia245 opened this issue Nov 18, 2016 · 0 comments

Comments

@zhongxia245
Copy link
Owner

时间:2016-11-18 15:51:49
类别:React

What's the difference between “super()” and “super(props)” in React when using es6 classes?

class MyComponent extends React.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.

本地测试

情况1: 不写 super(props)


结果

情况2: 写 super(props)


和答案说的一样,如果想在 constructor 里面使用 this.props.xxx 就需要写, 不然可以不写

参看文章

  1. http://stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-in-react-when-using-e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant