React Native之prop-types進(jìn)行屬性確認(rèn)詳解
屬性確認(rèn)的作用
使用 React Native 創(chuàng)建的組件是可以復(fù)用的,所以我們開發(fā)的組件可能會(huì)給項(xiàng)目組其他同事使用。但別人可能對(duì)這個(gè)組件不熟悉,常常會(huì)忘記使用某些屬性,或者某些屬性傳遞的數(shù)據(jù)類型有誤。
因此我們可以在開發(fā) React Native 自定義組件時(shí),可以通過屬性確認(rèn)來聲明這個(gè)組件需要哪些屬性。這樣,如果在調(diào)用這個(gè)自定義組件時(shí)沒有提供相應(yīng)的屬性,則會(huì)在手機(jī)與調(diào)試工具中彈出警告信息,告知開發(fā)者該組件需要哪些屬性。
React Native已經(jīng)升級(jí)到0.51.0了,版本升級(jí)很快,但是對(duì)老項(xiàng)目也會(huì)有一些問題,常見的就是屬性找不到的問題。例如:
主要原因是隨著React Native的升級(jí),系統(tǒng)廢棄了很多的東西,過去我們可以直接使用 React.PropTypes 來進(jìn)行屬性確認(rèn),不過這個(gè)自 React v15.5 起就被移除了,轉(zhuǎn)而使用prop-types庫來進(jìn)行替換
屬性確認(rèn)
屬性確認(rèn)的作用
使用 React Native 創(chuàng)建的組件是可以復(fù)用的,所以我們開發(fā)的組件可能會(huì)給項(xiàng)目組其他同事使用。但別人可能對(duì)這個(gè)組件不熟悉,常常會(huì)忘記使用某些屬性,或者某些屬性傳遞的數(shù)據(jù)類型有誤。因此我們可以在開發(fā) React Native 自定義組件時(shí),可以通過屬性確認(rèn)來聲明這個(gè)組件需要哪些屬性。
注意:為了保證 React Native 代碼高效運(yùn)行,屬性確認(rèn)僅在開發(fā)環(huán)境中有效,正式發(fā)布的 App 運(yùn)行時(shí)是不會(huì)進(jìn)行檢查的。
prop-types 庫使用
和其他的第三方庫使用類似,prop-types的安裝首先進(jìn)入項(xiàng)目根目錄,執(zhí)行如下代碼安裝 prop-types 庫:
npm install --save prop-types
然后在需要使用PropTypes屬性的地方引入:
import PropTypes from 'prop-types';
例子
例如,我們寫一個(gè)導(dǎo)航欄的例子,效果如下:

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,要求屬性是可渲染節(jié)點(diǎn)。例如:
屬性: PropTypes.node,
3,要求屬性是某個(gè) React 元素。例如:
屬性: PropTypes.element,
4,要求屬性是某個(gè)指定類的實(shí)例。例如:
屬性: PropTypes.instanceOf(NameOfAClass),
5,要求屬性取值為特定的幾個(gè)值。例如:
屬性: PropTypes.oneOf(['value1', 'value2']),
6,要求屬性可以為指定類型中的任意一個(gè)。例如:
屬性: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.number,
PropTypes.instanceOf(NameOfAClass),
])
7,要求屬性為指定類型的數(shù)組。例如:
屬性: PropTypes.arrayOf(PropTypes.number),
8,要求屬性是一個(gè)有特定成員變量的對(duì)象。例如:
屬性: PropTypes.objectOf(PropTypes.number),
9,要求屬性是一個(gè)指定構(gòu)成方式的對(duì)象。例如:
屬性: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number,
}),
10,屬性可以是任意類型。例如:
屬性: PropTypes.any
將屬性聲明為必須
使用關(guān)鍵字 isRequired 聲明它是必需的。
屬性: PropTypes.array.isRequired,
屬性: PropTypes.any.isRequired,
屬性: PropTypes.instanceOf(NameOfAClass).isRequired,
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- react native基于FlatList下拉刷新上拉加載實(shí)現(xiàn)代碼示例
- react native 獲取地理位置的方法示例
- react-native動(dòng)態(tài)切換tab組件的方法
- react-native android狀態(tài)欄的實(shí)現(xiàn)
- react-native封裝插件swiper的使用方法
- react-native-video實(shí)現(xiàn)視頻全屏播放的方法
- React Native 自定義下拉刷新上拉加載的列表的示例
- ReactNative實(shí)現(xiàn)Toast的示例
- ReactNative中使用Redux架構(gòu)總結(jié)
- React Native react-navigation 導(dǎo)航使用詳解
- react native 仿微信聊天室實(shí)例代碼
相關(guān)文章
react中實(shí)現(xiàn)將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換
這篇文章主要介紹了react中實(shí)現(xiàn)將一個(gè)視頻流為m3u8格式的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情
這篇文章主要介紹了React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情,每一個(gè)?hook?函數(shù)都有對(duì)應(yīng)的?hook?對(duì)象保存狀態(tài)信息,更多詳細(xì)分析,需要的朋友可以參考一下2022-07-07
React?Context源碼實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了React?Context源碼實(shí)現(xiàn)原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
react通過組件拆分實(shí)現(xiàn)購物車界面詳解
這篇文章主要介紹了react通過組件拆分來實(shí)現(xiàn)購物車頁面的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

