What's the difference between “super()” and “super(props)” in React when using es6 classes?

后端 未结 10 1955
不思量自难忘°
不思量自难忘° 2020-11-22 09:01

When is it important to pass props to super(), and why?

class MyComponent extends React.Component {
  constructor(props) {
    supe         


        
10条回答
  •  抹茶落季
    2020-11-22 09:41

    For react version 16.6.3, we use super(props) to initialize state element name : this.props.name

    constructor(props){
        super(props);        
    }
    state = {
      name:this.props.name 
        //otherwise not defined
    };
    

提交回复
热议问题