美文网首页
React第二课

React第二课

作者: tonytong | 来源:发表于2017-01-13 11:33 被阅读0次

PropTypes:javaScript是一门弱类型的语言,为方便代码维护和测试,通过propType指明数据类型。当传入的类型和声明的类型不匹配时,控制台会主动报错。示例代码如下:

var MyTitle = React.createClass({

 propTypes :{ //指明数据类型

className:React.PropTypes.string,  

  title: React.PropTypes.string,

},   

 getDefaultProps : function () {

    return {      title : 'Hello World'    };  

}, 

 render:function(){    

return (<p>{this.props.title}<p/>); 

}

});

ReactDOM.render(,document.getElementById('example'));

当我传入的number类型的参数时会报如下错误。

react.js:18794 Warning: Failed propType: Required prop `className` was not specified in `MyTitle`.

react.js:18794 Warning: Failed propType: Invalid prop `title` of type `number` supplied to `MyTitle`, expected `string`.

相关文章

网友评论

      本文标题:React第二课

      本文链接:https://www.haomeiwen.com/subject/udtsbttx.html