2016-03-07 52 views
8

想我再也以下類:ES6課堂中超級(道具)的召喚是否重要?

class Tabs extends React.Component { 
    displayName: Tabs; 

    static propTypes = { 
    selected: React.PropTypes.number, 
    children: React.PropTypes.oneOfType([ 
    React.PropTypes.array, 
    React.PropTypes.element 
    ]).isRequired 
    }; 

    constructor() { 
    super(); 
    this.state = { 
    selected: 0, 
    maxSelected: 0 
    }; 

    render() { 
    return(
     <div> 
     {this.props.selected} 
     {this.props.children} 
     </div> 
    ); 
    } 
}; 

我想知道,如果通過下面的構造是很重要的:

constructor(props) { 
    super(props); 
} 

我當前的代碼工作得很好,但我想知道,如果這是一個很好的做法。

+0

不是,事實上甚至有一條嚴厲的規則來禁止這個http://eslint.org/docs/rules/no-useless-constructor – azium

+1

可能的重複[「super()」和「超級(道具)「在React使用ES6類?](http://stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-in-react-when-using-e) – XML

回答

15

根據Ben Alpert with the React team,如果您打算在構造函數中使用this.props,則只需將道具傳遞給構造函數。在構造函數被調用後,React將外部的道具粘貼到組件上。