使用 es6 类时,React 中的 “super()” 和 “super(props)” 有什么区别?

回答·5
最热
最新
  • 貌似默认继承的 Component 类 也会帮你做 super(props)
  • 建议都写上 props ,正常只有你要在构造函数中要使用才加
  • 如果写了 constructor,那就要写 super,如果 constructor 里面用到 this.props,那就要用之前调一下 super(props),才能确保继承父类的构造函数。
  • 看你需不需要在 constructor 里使用 props
  • 如果写了 constructor,那就要写 super,如果 constructor 里面用到 this.props,那就要用之前调一下 super(props),才能确保继承父类的构造函数。