首页 > 关于React组件类的propTypes属性

关于React组件类的propTypes属性

为什么console没有提示信息??

var MyTitle = React.createClass({
        propTypes:{
            title:React.PropTypes.isRequired,
        },
        render:function(){
            return <h1>{this.props.title}</h1>;
        }
    });

var data = 10;

ReactDOM.render(<MyTitle />,document.body);

因为校验只在development mode中生效:

As your app grows it's helpful to ensure that your components are used correctly. We do this by allowing you to specify propTypes. React.PropTypes exports a range of validators that can be used to make sure the data you receive is valid. When an invalid value is provided for a prop, a warning will be shown in the JavaScript console. Note that for performance reasons propTypes is only checked in development mode

看文档:Prop Validation

至于如何开启/禁用development mode,看这里:How to turn on/off ReactJS 'development mode'?

【热门文章】
【热门文章】