React-Native:Props
props
:屬性室抽,大多數(shù)的組件在創(chuàng)建時(shí)可以使用各種參數(shù)進(jìn)行設(shè)置辩棒,就是屬性props
贞绳。比如<Image>
組件的source
來(lái)指定要顯示的圖片的地址,以及使用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
函數(shù)中引用this.props
,然后按照需要進(jìn)行處理册招。
// 定義一個(gè)MyFirst組件
class MyFirst extends Component {
render(){
return (
<Text>Hello {this.props.name}</Text>
);
}
}
// 定義一個(gè)NewCom組件 里面使用MyFirst組件并設(shè)置props
class NewCom extends Component {
render(){
return (
<View>
<MyFirst name = 'Zchen'/>
<MyFirst name = 'Zjia'/>
</View>
);
}
}
// 注冊(cè)NewCom組件
AppRegistry.registerComponent("NewCom",()=>NewCom);
上面NewCom
使用MyFirst
組件時(shí)設(shè)置了name
屬性岔激。