谈谈react中的constructor和super的作用

在react中可以不写constructor,一旦写了constructor,就必须在此函数中写super(),此时组件才有自己的this,在组件的全局中都可以使用this关键字,否则如果只是constructor 而不执行 super() 那么以后的this都是错的!!!
更多constructor方法和super的作用

class Dividend extends React.Component {
    
    
     constructor(props) {
    
    
   		 super(props);  //子类继承父类的属性和方法
   		 this.state = {
    
    
   		 	 visible: false,
      		 selectedRowKeys: [],
        }
    }
}
export default Dividend;

猜你喜欢

转载自blog.csdn.net/m0_48076809/article/details/109239373