React Native之屬性類型檢查機制詳解 PropType 變成 prop-types
屬性確認的作用
使用 React Native 創建的組件是可以復用的,所以我們開發的組件可能會給項目組其他同事使用。但別人可能對這個組件不熟悉,常常會忘記使用某些屬性,或者某些屬性傳遞的數據類型有誤。
因此我們可以在開發 React Native 自定義組件時,可以通過屬性確認來聲明這個組件需要哪些屬性。這樣,如果在調用這個自定義組件時沒有提供相應的屬性,則會在手機與調試工具中彈出警告信息,告知開發者該組件需要哪些屬性。
React Native已經升級到0.51.0了,版本升級很快,但是對老項目也會有一些問題,常見的就是屬性找不到的問題。例如:
主要原因是隨著React Native的升級,系統廢棄了很多的東西,過去我們可以直接使用 React.PropTypes 來進行屬性確認,不過這個自 React v15.5 起就被移除了,轉而使用prop-types庫來進行替換
屬性確認
屬性確認的作用
使用 React Native 創建的組件是可以復用的,所以我們開發的組件可能會給項目組其他同事使用。但別人可能對這個組件不熟悉,常常會忘記使用某些屬性,或者某些屬性傳遞的數據類型有誤。因此我們可以在開發 React Native 自定義組件時,可以通過屬性確認來聲明這個組件需要哪些屬性。
註意:為了保證 React Native 代碼高效運行,屬性確認僅在開發環境中有效,正式發布的 App 運行時是不會進行檢查的。
prop-types 庫使用
和其他的第三方庫使用類似,prop-types的安裝首先進入項目根目錄,執行如下代碼安裝 prop-types 庫:
1 |
npm install --save prop-types
|
然後在需要使用PropTypes屬性的地方引入:
1 |
import PropTypes from ‘prop-types‘
|
例子
例如,我們寫一個導航欄的例子,效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import React, {
Component
} from ‘react‘
import {
StyleSheet,
View,
Animated,
Image,
TouchableOpacity,
TouchableNativeFeedback,
Platform
} from ‘react-native‘
import px2dp from ‘../utils/Utils‘
import Icon from ‘react-native-vector-icons/Ionicons‘
import PropTypes from ‘prop-types‘ ;
export default class NavBar extends Component{
static propTypes = {
title: PropTypes.string,
leftIcon: PropTypes.string,
rightIcon: PropTypes.string,
leftPress: PropTypes.func,
rightPress: PropTypes.func,
style: PropTypes.object
}
static topbarHeight = (Platform.OS === ‘ios‘ ? 64 : 44)
renderBtn(pos){
let render = (obj) => {
const { name, onPress } = obj
if (Platform.OS === ‘android‘ ){
return (
<TouchableNativeFeedback onPress={onPress} style={styles.btn}>
<Icon name={name} size={px2dp(26)} color= "#fff" />
</TouchableNativeFeedback>
)
} else {
return (
<TouchableOpacity onPress={onPress} style={styles.btn}>
<Icon name={name} size={px2dp(26)} color= "#fff" />
</TouchableOpacity>
)
}
}
if (pos == "left" ){
if ( this .props.leftIcon){
return render({
name: this .props.leftIcon,
onPress: this .props.leftPress
})
} else {
// return (<ImageButton style={styles.btn} source={require(‘../images/ic_back_white.png‘)}/>)
return (<View style={styles.btn}/>)
}
} else if (pos == "right" ){
if ( this .props.rightIcon){
return render({
name: this .props.rightIcon,
onPress: this .props.rightPress
})
} else {
return (<View style={styles.btn}/>)
}
}
}
render(){
return (
<View style={[styles.topbar, this .props.style]}>
{ this .renderBtn( "left" )}
<Animated.Text numberOfLines={1} style={[styles.title, this .props.titleStyle]}>{ this .props.title}</Animated.Text>
{ this .renderBtn( "right" )}
</View>
)
}
}
const styles = StyleSheet.create({
topbar: {
height: NavBar.topbarHeight,
backgroundColor: "#06C1AE" ,
flexDirection: ‘row‘ ,
justifyContent: ‘space-between‘ ,
alignItems: ‘center‘ ,
paddingTop: (Platform.OS === ‘ios‘ ) ? 20 : 0,
paddingHorizontal: px2dp(10)
},
btn: {
width: 22,
height: 22,
justifyContent: ‘center‘ ,
alignItems: ‘center‘
},
title:{
color: "#fff" ,
fontWeight: "bold" ,
fontSize: px2dp(16),
marginLeft: px2dp(5),
}
});
|
語法
1,要求屬性是指定的 JavaScript 基本類型。例如:
屬性: PropTypes.array,
屬性: PropTypes.bool,
屬性: PropTypes.func,
屬性: PropTypes.number,
屬性: PropTypes.object,
屬性: PropTypes.string,
2,要求屬性是可渲染節點。例如:
屬性: PropTypes.node,
3,要求屬性是某個 React 元素。例如:
屬性: PropTypes.element,
4,要求屬性是某個指定類的實例。例如:
屬性: PropTypes.instanceOf(NameOfAClass),
5,要求屬性取值為特定的幾個值。例如:
屬性: PropTypes.oneOf([‘value1‘, ‘value2‘]),
6,要求屬性可以為指定類型中的任意一個。例如:
屬性: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.number,
PropTypes.instanceOf(NameOfAClass),
])
7,要求屬性為指定類型的數組。例如:
屬性: PropTypes.arrayOf(PropTypes.number),
8,要求屬性是一個有特定成員變量的對象。例如:
屬性: PropTypes.objectOf(PropTypes.number),
9,要求屬性是一個指定構成方式的對象。例如:
屬性: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number,
}),
10,屬性可以是任意類型。例如:
屬性: PropTypes.any
將屬性聲明為必須
使用關鍵字 isRequired 聲明它是必需的。
屬性: PropTypes.array.isRequired,
屬性: PropTypes.any.isRequired,
屬性: PropTypes.instanceOf(NameOfAClass).isRequired,
React-Native中props具體使用詳解
props就是屬性,是為了描述一個組件的特征而存在的。它是父組件傳遞給子組件的。
使用props
通過上一個頁面傳遞
新建一個 PropsTest.js 文件
1 2 3 4 5 |
exprot default class PropsTestextendesComponent{
render(){
return <Text>{ this .props.name}</Text>
}
}
|
在上一個頁面中使用PropsTest組件
1 2 3 4 5 |
import PropsTest from ‘./PropsTest‘
<PropsTest
name = ‘XiaoMing‘
/>
|
註意: 上方代碼,均為代碼片段。
默認屬性,以及它的作用
由於props的屬性都是上個頁面傳遞的,所以無法修改它。但是我們可以在PropsTest文件中,給props定義一些默認的值。
1 2 3 4 5 6 7 8 |
exprot default class PropsTestextends Component{
static defaultProps={
name: ‘XiaoHong‘
}
render(){
return <Text>{ this .props.name}</Text>
}
}
|
註意,defaultProps 需要使用static關鍵字來做靜態修飾。這樣,如果上個頁面沒有傳值,則顯示的是這個默認的屬性。
對props進行約束和檢查
1 2 3 4 5 6 7 8 9 10 11 12 13 |
exprot default class PropsTestextends Component{
static defaultProps={
name: ‘XiaoHong‘
}
static propTypes={
name: PropTypes.string,
age: PropTypes.number,
sex: PropTypes.string.isRequired
}
render(){
return <Text>{ this .props.name}</Text>
}
}
|
對props裏面的屬性進行類型判斷,可以使用propTypes來做到,同樣需要使用static關鍵字來修飾。
isRequired 可以指定必填項
註意:
propTypes屬性 在 react 包中,需要先導入才能使用。
props延伸操作符
ES6的最新語法
假如我們的組件需要好多屬性,如下:
1 2 3 4 5 6 7 8 9 |
params = {name: ‘XiaoZhang‘ , age: 18, sex: ‘男‘ }
// 如果需要傳遞給下一個頁面需要:
<PropsTest
name = {params.name}
sex = {params.sex}
age = {params.age}
/>
// 等等,這樣如果屬性特別多,代碼將會變得難以維護。
|
在ES6中可以使用最新的延伸操作符特性
1 2 3 |
<PropsTest
{...params}
/>
|
非常的簡潔
props解構賦值
ES6的最新語法
通過延伸操作符傳遞的對象,在另一個組件中想要從中獲取某幾個來使用,可以用解構賦值的方式
1 2 3 4 5 6 7 8 9 |
var {name, age} = params;
// 其他地方就可以直接引用name和age了
{name}或{age}
// 這麽做的好處,同樣是不需要使用如下的傳統方式
{params.name}或{params.age}
|
以上就是本文的全部內容,希望對大家的學習有所幫助
React Native之屬性類型檢查機制詳解 PropType 變成 prop-types