zoukankan      html  css  js  c++  java
  • ReactNative--属性默认设置

    属性验证 propTypes

    组件类的属性

    用于验证组件实例的属性是否符合要求

        var ShowTitle = React.createClass({
          propTypes:{
            title: React.PropTypes.string.isRequired
          },
          render: function () {
            return <h1>{this.props.title}</h1>
          }
        });
    
        ReactDOM.render(
          <ShowTitle title=123/>,
          document.getElementById("container")
        )

    下面传的123是数字,不是字符串,所以会报错

    设置组件属性的默认值

    通过实现组件的getDefaultProps方法,对属性设置默认值

        var MyTitle = React.createClass({
          getDefaultProps:function () {
            return {
              title:"lanou"
            };
          },
          render:function () {
            return <h1>{this.props.title}</h1>
          }
        });
    
        ReactDOM.render(
          <MyTitle />,
          document.getElementById("container")
        );
  • 相关阅读:
    Python
    Python
    Python
    Python
    Python
    Python
    Python
    python
    对象
    py常用模块
  • 原文地址:https://www.cnblogs.com/chebaodaren/p/6430365.html
Copyright © 2011-2022 走看看