What does calling super() in a React constructor do?

前端 未结 6 497
夕颜
夕颜 2020-11-30 20:55

Learning React from the docs and came across this example:

class Square extends React.Component {
  constructor() {
    super();
    this.state = {
      val         


        
6条回答
  •  难免孤独
    2020-11-30 21:56

    Worth adding that super() is short for superclass contructor, a concept from inheritance.

    By default the class Square inherits the constructor from its superclass React.Component.

    The inherited constructor can be overridden by declaring a new constructor() method.

    If the intention is to extend rather than override the superclass constructor then it must be explicitly invoked using super().

提交回复
热议问题