Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React ES6 class constructor super() #8

Open
LiuL0703 opened this issue Jan 2, 2018 · 0 comments
Open

React ES6 class constructor super() #8

LiuL0703 opened this issue Jan 2, 2018 · 0 comments
Labels

Comments

@LiuL0703
Copy link
Owner

LiuL0703 commented Jan 2, 2018

当我们在写React时候 会用到ES6中的class语法 ,比较常见的情况如下:

class MyClass extends React.Component{
    constructor(){
        super()
    }
}

这里有两个问题:

  1. 是否有必要在constructor中调用super()函数?

  2. 调用super()super(props) 有何区别 ?

解答 Q1:

Always call super() if you have a constructor and don't worry about it if you don't have a constructor

只有当你有一个constructor时候调用super()才是必须的 看代码:

class MyClass extends React.component{
    render(){
    	return <div>Hello {this.props.world}</div>
    }
}

上述代码完全符合规定所以你其实并没有必要去为你创建的每个React Component 调用super() 话分两头 如果你的代码中有constrctor你就必须调用super()

class MyClass extends React.component {
    constructor(){
        console.log(this) //Error: 'this' is not allowed before super()
    }
}

出现上述错误的原因是 super()未被调用之前 this还未被初始化 (uninitialized) [更多]
或许聪敏的你会想着 使用一个空的constructor从而摆脱super()

class MyClass extends React.component {
    constructor(){} // Error: missing super() call in constructor
}

ES6的class的constructors如果属于子类就 必须调用super()方法 所以一旦你的代码有 constructor你就必须调用用super()

解答Q 2:

Call super(props) only if you want to access this.props inside the constructor. React automatically set it for you if you want to access it anywhere else.

假使你想获取到constructor中的this.props 你就必须调用super(props) 然后React就会自动为你自动为你配置好它 以便你可以在随便什么地方调用它

看一下使用super() super(props) 的不同 :

class MyClass extends React.component{
    constructor(props){
        super();
        console.log(this.props); // this.props is undefined

    }
}

当使用super(props)时 你可以从constructor中获取到this.props

class MyClass extends React.component{
    constructor(props){
        super(props);
        console.log(this.props); // prints out whatever is inside props
    }
}

当然还有一点 当你想在其他地方使用它时 也没有必要将props传递到constructor中 React会自动为你设置好它 [更多]

class MyClass extends React.component{
    render(){
        // There is no need to call `super(props)` or even having a constructor 
        // this.props is automatically set for you by React 
        // not just in render but another where else other than the constructor
        console.log(this.props);  // it works!
    }
}

我的理解是 总之 需要绑定 this. 方法或是需要在 constructor 使用操作 props 定义 state,就需要 constructor ,否则 例如在其他方法中(如 render())使用 this.props 则没必要要使用 constructor

原文链接: React ES6 class constructor super()

@LiuL0703 LiuL0703 added the React label Jan 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant