关于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);
阅读 3.6k
1 个回答

因为校验只在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'?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题