ReactNative之prop-types進行屬性確認詳解

屬性確認的作用

成都創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站制作、網(wǎng)站建設(shè),集網(wǎng)站策劃、網(wǎng)站設(shè)計、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文平臺等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計,讓網(wǎng)站在運行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。

使用 React Native 創(chuàng)建的組件是可以復(fù)用的,所以我們開發(fā)的組件可能會給項目組其他同事使用。但別人可能對這個組件不熟悉,常常會忘記使用某些屬性,或者某些屬性傳遞的數(shù)據(jù)類型有誤。

因此我們可以在開發(fā) React Native 自定義組件時,可以通過屬性確認來聲明這個組件需要哪些屬性。這樣,如果在調(diào)用這個自定義組件時沒有提供相應(yīng)的屬性,則會在手機與調(diào)試工具中彈出警告信息,告知開發(fā)者該組件需要哪些屬性。

React Native已經(jīng)升級到0.51.0了,版本升級很快,但是對老項目也會有一些問題,常見的就是屬性找不到的問題。例如:

React Native之prop-types進行屬性確認詳解 

主要原因是隨著React Native的升級,系統(tǒng)廢棄了很多的東西,過去我們可以直接使用 React.PropTypes 來進行屬性確認,不過這個自 React v15.5 起就被移除了,轉(zhuǎn)而使用prop-types庫來進行替換

屬性確認

屬性確認的作用

使用 React Native 創(chuàng)建的組件是可以復(fù)用的,所以我們開發(fā)的組件可能會給項目組其他同事使用。但別人可能對這個組件不熟悉,常常會忘記使用某些屬性,或者某些屬性傳遞的數(shù)據(jù)類型有誤。因此我們可以在開發(fā) React Native 自定義組件時,可以通過屬性確認來聲明這個組件需要哪些屬性。

注意:為了保證 React Native 代碼高效運行,屬性確認僅在開發(fā)環(huán)境中有效,正式發(fā)布的 App 運行時是不會進行檢查的。

prop-types 庫使用

和其他的第三方庫使用類似,prop-types的安裝首先進入項目根目錄,執(zhí)行如下代碼安裝 prop-types 庫:

npm install --save prop-types

然后在需要使用PropTypes屬性的地方引入:

import PropTypes from 'prop-types';

例子

例如,我們寫一個導(dǎo)航欄的例子,效果如下:

React Native之prop-types進行屬性確認詳解

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é)點。例如:

屬性: 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,要求屬性為指定類型的數(shù)組。例如:

屬性: PropTypes.arrayOf(PropTypes.number),

8,要求屬性是一個有特定成員變量的對象。例如:

屬性: PropTypes.objectOf(PropTypes.number),

9,要求屬性是一個指定構(gòu)成方式的對象。例如:

屬性: 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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

名稱欄目:ReactNative之prop-types進行屬性確認詳解
文章源于:http://muchs.cn/article6/pipsog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站策劃、用戶體驗、自適應(yīng)網(wǎng)站小程序開發(fā)、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司