RN:Props

React-Native:Props

props:属性,大多数的组件在创建时可以使用各种参数进行设置,就是属性props。比如<Image>组件的source来指定要显示的图片的地址,以及使用style控制尺寸等等。

let picUrl = {
    uir:'https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=b4c574eab012c8fcabf3f0cdcc0292b4/8326cffc1e178a827f4f796aff03738da877e88d.jpg'
}
<Image source={picUrl} style={{width:190,height:110}}/>
  • 自定义的组件使用props。只需要在render函数中引用this.props,然后按照需要进行处理。
// 定义一个MyFirst组件
class MyFirst extends Component {
  render(){
    return (
      <Text>Hello {this.props.name}</Text>
    );
  }
}
// 定义一个NewCom组件 里面使用MyFirst组件并设置props
class NewCom extends Component {
  render(){
    return (
      <View>
        <MyFirst name = 'Zchen'/>
        <MyFirst name = 'Zjia'/>
      </View>
    );
  }
}
// 注册NewCom组件
AppRegistry.registerComponent("NewCom",()=>NewCom);

上面NewCom使用MyFirst组件时设置了name属性。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容