props(属性)
是组件自身的属性,props中的属性与组件属性一一对应。负责传递信息
1、父组件向子组件传递数据
//定义webname组件,负责输出名字var webname = react.createclass({  render : function() {    return <h1>{this.props.webname} </h1>;  }})//定义weblink组件,负责跳转链接var weblink = react.createclass({  render : function() {    return <a href={this.props.weblink}>{this.props.weblink}</a>  }}) var webshow = react.createclass({  render : function(){    <div>      <webname webname={this.props.webname}/>      <weblink weblink={this.props.weblink}/>    </div>  }}) //渲染reactdom.render{  return function() {    <webshow webname = "hellp" weblink = "www.baidu.com" />,      document.getelementbyid("container")  }}
2、设置默认属性
通过static defaultprops = {}这种固定的格式来给一个组件添加默认属性
export default class myview extends component {  static defaultprops = {    age: 12,    sex: '男'  }   render() {    return <text        style={{backgroundcolor: 'cyan'}}>      你好{this.props.name}{'\n'}年龄{this.props.age}{'\n'}性别{this.props.sex}    </text>  }}
3、属性检查
通过 static proptypes = {} 这种固定格式来设置属性的格式,比如说我们将 age 设置为 number 类型
var title = react.createclass({  proptypes={    //title类型必须是字符串    title : react.proptypes.string.isrequired  },  render : function() {    return <h1>{this.props.title}</h1>  }})
延展操作符 ... 是 es6 语法的新特性。...this.porps,一种语法,将父组件中全部属性复制给子组件
2 父组件向子组件传递调用函数,用来通知父组件消息。
3 用来作为子组件逻辑判断的标示,渲染的样式等
4 children,不是跟组件对应的属性,表示组件所有子节点。
//定义webname组件,负责输出名字var listcompont = react.createclass({  render : function() {    return    <ul>      {        /**         * 列表项内容不固定,需要在创建模版时确定。利用this.props.children传递显示内容         * 获取到内容后,需要遍历children,逐项设置。利用react.children.map方法         **/         react.children.map(this.props.children,function(child) {            //child是遍历得到父组件子节点            return <li>{child}</li>;        })      }    </ul>;  }})//渲染reactdom.render{  {    <listcompont>      <h1>hello</h1>      <a href="link">"www.baidu.com"</a>    </listcompont>  },document.getelementbyid("container")}
总结:以上就是本篇文章的全部内容了,希望对大家有所帮助
以上就是props属性如何设置的详细内容。
   
 
   