React 组件的默认状态(defaultState)

Posted 1012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React 组件的默认状态(defaultState)相关的知识,希望对你有一定的参考价值。

基本类型组件:

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‘
};

以上是关于React 组件的默认状态(defaultState)的主要内容,如果未能解决你的问题,请参考以下文章