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

前端 未结 6 484
夕颜
夕颜 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:47

    To use this keyword we should use the super keyword before it. Why? super is used to call the parent class's constructor.

    Now why do we need to call the parent's constructor? The answer is to initialize the properties values which we are referring through this keyword.

提交回复
热议问题