成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[譯]React ES6 class constructor super()

justjavac / 983人閱讀

摘要:當(dāng)我們在寫時(shí)候會用到中的語法比較常見的情況如下這里有兩個(gè)問題是否有必要在中調(diào)用函數(shù)調(diào)用和有何區(qū)別解答只有當(dāng)你有一個(gè)時(shí)候調(diào)用才是必須的看代碼上述代碼完全符合規(guī)定所以你其實(shí)并沒有必要去為你創(chuàng)建的每個(gè)調(diào)用話分兩頭如果你的代碼中有你就必須調(diào)用出現(xiàn)上

當(dāng)我們在寫React時(shí)候 會用到ES6中的class語法 ,比較常見的情況如下:

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

這里有兩個(gè)問題:

是否有必要在constructor中調(diào)用super()函數(shù)?

調(diào)用super()super(props) 有何區(qū)別 ?

解答 Q1:

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

只有當(dāng)你有一個(gè)constructor時(shí)候調(diào)用super()才是必須的 看代碼:

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

上述代碼完全符合規(guī)定所以你其實(shí)并沒有必要去為你創(chuàng)建的每個(gè)React Component 調(diào)用super() 話分兩頭 如果你的代碼中有constructor你就必須調(diào)用super()

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

出現(xiàn)上述錯(cuò)誤的原因是 super() 未被調(diào)用之前 this 還未被初始化 (uninitialized) 了解更多
或許聰敏的你會想著 使用一個(gè)空的constructor從而擺脫super()

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

ES6的 class 的constructors如果屬于子類就 必須調(diào)用super()方法 所以一旦你的代碼有
constructor 你就必須調(diào)用用 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 你就必須調(diào)用super(props) 然后React就會自動為你自動為你配置好它 以便你可以在隨便什么地方調(diào)用它

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

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

    }
}

當(dāng)使用super(props)時(shí) 你可以從constructor中獲取到this.props

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

當(dāng)然還有一點(diǎn) 當(dāng)你想在其他地方使用它時(shí) 也沒有必要將props傳遞到constructor中 React會自動為你設(shè)置好它 了解更多

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()

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/91387.html

相關(guān)文章

  • []React ES6 class constructor super()

    摘要:會自行設(shè)置在組件的其他地方以供訪問。將傳入的作用是可以使你在內(nèi)訪問它完善后如果你只是想在別處訪問它,是不必傳入的,因?yàn)闀詣訛槟阍O(shè)置好 原博文地址: http://cheng.logdown.com/posts/2016/03/26/683329 當(dāng)我們像下面這樣使用React的ES6 class語法創(chuàng)建一個(gè)組件的時(shí)候: class MyClass extends React.comp...

    terasum 評論0 收藏0
  • []在 React.js 中使用 ES6+

    摘要:如果是在中,我們也許只能這樣做但是,在中,我們不僅可以在對象字面量屬性的定義中使用表達(dá)式,還有使用使用字符串模板析構(gòu)擴(kuò)展運(yùn)算符我們在編寫組件的過程中,經(jīng)常遇到要從父組件要把自己的很多屬性多傳給子組件的情況。 原文地址: http://babeljs.io/blog/2015/06/07/react-on-es6-plus/ showImg(http://7xiyp1.com1.z0.g...

    Enlightenment 評論0 收藏0
  • [] React 組件中綁定回調(diào)

    摘要:好的方案在構(gòu)造函數(shù)中仍然使用,現(xiàn)在我們只要繞過每次渲染都要生成新的函數(shù)的問題就可以了。我們可以通過只在構(gòu)造函數(shù)中綁定回調(diào)的上下問來解決這個(gè)問題,因?yàn)闃?gòu)造函數(shù)只會調(diào)用一次,而不是每次渲染都調(diào)用。 原文:Binding callbacks in React components 在組件中給事件綁定處理函數(shù)是很常見的,比如說每當(dāng)用戶點(diǎn)擊一個(gè)button的時(shí)候使用console.log打印一些...

    Lin_R 評論0 收藏0
  • 】Handling Events

    摘要:如果你使用實(shí)驗(yàn)性屬性初始化語法,你能用這方法來正確綁定回調(diào)函數(shù)的綁定這語法在中默認(rèn)支持。然而,如果這回調(diào)函數(shù)是作為一個(gè)傳遞到更下一級的組件中的時(shí)候,些組件可能會做一個(gè)額外的重新渲染。 下面是react官方文檔的個(gè)人翻譯,如有翻譯錯(cuò)誤,請多多指出原文地址:https://facebook.github.io/re... Handling events with React element...

    sugarmo 評論0 收藏0
  • []JavaScript ES6 class指南

    摘要:前言又稱通過一些新的關(guān)鍵字,使類成為了中一個(gè)新的一等公民。類聲明在中,有兩個(gè)聲明類的方式。在使用了新的關(guān)鍵字后在底層,所做的,也只是將這個(gè)方法添加為構(gòu)造函數(shù)的一個(gè)屬性。在想要調(diào)用父類的構(gòu)造函數(shù)時(shí),你可以簡單地將關(guān)鍵字視作一個(gè)函數(shù)使用,如。 前言 EcmaScript 2015 (又稱ES6)通過一些新的關(guān)鍵字,使類成為了JS中一個(gè)新的一等公民。但是目前為止,這些關(guān)于類的新關(guān)鍵字僅僅是建...

    CoderDock 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<