React 组件的默认状态(defaultProps)

基本类型组件:

class MyComponent extends React.Component {
    constructor(props){
        super(props);
        this.state={
            name:props.name
        };
    }
}

MyComponent.defaultProps={
    name:'default name'
};

功能型组件

const MyFunctionalComponent=(props)=>{
    return (
        <div>
            <p>{props.name}</p>
        </div>
    );
}

MyFunctionalComponent.defaultProps={
    name:'default name'
};

posted @ 2017-11-30 10:47  青玄鸟  阅读(14274)  评论(0)    收藏  举报