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

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

    When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.

提交回复
热议问题