当前位置: 移动技术网 > IT编程>开发语言>JavaScript > React Native之prop-types进行属性确认详解

React Native之prop-types进行属性确认详解

2018年02月22日  | 移动技术网IT编程  | 我要评论

属性确认的作用

使用 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 库:

npm install --save prop-types

然后在需要使用proptypes属性的地方引入:

import proptypes from '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,要求属性是可渲染节点。例如:

属性: 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,

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网