react设置默认state和默认props

1.默认状态设置

1.constructor (ES6)

constructor(props) {
    this.state = {
        n: ...
    }
}

2.getInitialState (ES5)

只能用在React.createClass中,extends React.Component不行

2.默认props设置

1.组件外部 (ES6)

component.defaultProps = {
    name: '...'
}

2.组件内部 (ES7,必须开启ES7的babel支持)

static defaultProps = {
    name: '...'
}

3. getDefaultProps (ES5)

只能用在React.createClass中,extends React.Component不行

原文地址:https://www.cnblogs.com/mengff/p/9603878.html