自定义React Native组件CheckBox

RN中的自带的CheckBox组件只能用在android中,这就需要ios端要自己写,很麻烦。所以这里就自己封装了一个CheckBox,取名叫CheckBtn。话不多说,直接上代码:

import React, {Component} from 'react';
import Icon from 'react-native-vector-icons/AntDesign'
import {View,Text,TouchableOpacity} from 'react-native'

export default class CheckBtn extends Component{
    constructor(props){
        super(props);
        this.state = ({
            setCheckd:false
        })
    }

    componentDidMount(){

        let flag = this.props.checked;
        this.setState({
            setCheckd:flag
        })
    }

    render(){
            return(
                <View style={{flexDirection:'row',alignItems:'center'}}>
                {
                    this.state.setCheckd ?
                    <TouchableOpacity onPress={()=>{
                        this.props.onCheckChange(false);
                        this.setState({
                            setCheckd:false
                        })
                        }}>
                        <Icon name='checkcircle' size={24} color='orangered'/>
                    </TouchableOpacity>
                    :
                    <TouchableOpacity onPress={()=>{
                        this.props.onCheckChange(true);
                        this.setState({
                            setCheckd:true
                        })
                        }}>
                        <Icon name='checkcircleo' size={24} color='#999'/>
                    </TouchableOpacity>
                }   
                    <Text style={{marginLeft:6}}>{this.props.label}</Text>
                </View>
            )
        }
        
    }

使用方法:首先import导入,然后:

<CheckBtn checked={false} label='xxxxx' onCheckChange={(value)=>{
                               
                            }}></CheckBtn>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值