美文网首页
React基础

React基础

作者: tonytong | 来源:发表于2017-11-05 15:36 被阅读0次

1.指定类成员属性

1.新版React中移除了PropTypes属性,需通过'prop-types'库完成类型指定
比如在封装自定义Toast中有这么使用

import {PropTypes} from 'prop-types';
Toast.propTypes = {
    style: View.propTypes.style,
    position:PropTypes.oneOf([
        'top',
        'center',
        'bottom',
    ]),
    textStyle: Text.propTypes.style,
    positionValue:PropTypes.number,
    fadeInDuration:PropTypes.number,
    fadeOutDuration:PropTypes.number,
    opacity:PropTypes.number
}

其中style指定为视图样式类型;position指定为枚举类型;textStyle指定为文本样式类型,positionValue指定为基本数据类型中number类型;......

2.类型检测的方法

let delay = typeof duration === 'undefined' ? this.duration : duration;
或者
this.duration = typeof duration === 'number' ? duration : DURATION.LENGTH_SHORT;
`

相关文章

网友评论

      本文标题:React基础

      本文链接:https://www.haomeiwen.com/subject/kezcmxtx.html