其他分享
首页 > 其他分享> > ReactNative 自定义组件

ReactNative 自定义组件

作者:互联网

定义属性

import PropTypes from 'prop-types'

  static propTypes = {
        title:PropTypes.string.isRequired,// 必选
        titleColor:PropTypes.any,
        onPress:PropTypes.func,
        selected:PropTypes.bool,
        icon:PropTypes.any
    }
    static defaultProps = {
        titleColor:'#4b4b4f',//可选默认值
        onPress:undefined,
        
        selected:false,
        icon:""
    }

  render() { 
        return (  
            <TouchableOpacity activeOpacity={0.5}  style={[styles.container]} onPress={this.onPress}>
             <View >
                <View style={[styles.imageBg,{backgroundColor:this.props.selected?'#45bfbc':'transparent'}]}>
                    <Image source={this.props.icon} style={{width:22,height:22}} resizeMode='contain'></Image>
                </View>
                <Text style={[styles.text,{color:this.props.titleColor}]}>{this.props.title}</Text>
             </View>

            </TouchableOpacity>
   
        );
    }

标签:自定义,title,selected,PropTypes,static,组件,onPress,any,ReactNative
来源: https://www.cnblogs.com/cnman/p/15040317.html