admin管理员组

文章数量:1399950

I am new to react native and trying to add a check box in the view but I am unable to get check box in the view of react native.

Thanks in advance.

import React from 'react';
import {View, CheckBox } from 'react-native';

export default class SimpleCheckBox extends React.Component{
   constructor(){
     super();
   }

  render(){
     return(
        <View>
         <CheckBox value={true} onValueChange={() => console.log("value might change")}/> 
        </View>
     )
  }
}

I am new to react native and trying to add a check box in the view but I am unable to get check box in the view of react native.

Thanks in advance.

import React from 'react';
import {View, CheckBox } from 'react-native';

export default class SimpleCheckBox extends React.Component{
   constructor(){
     super();
   }

  render(){
     return(
        <View>
         <CheckBox value={true} onValueChange={() => console.log("value might change")}/> 
        </View>
     )
  }
}
Share Improve this question edited Oct 10, 2017 at 16:24 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked Oct 10, 2017 at 14:02 KumarKumar 2801 gold badge4 silver badges18 bronze badges 2
  • What version of RN that you currently use? – Wanda Ichsanul Isra Commented Oct 10, 2017 at 14:12
  • React Native 0.48 – Kumar Commented Oct 10, 2017 at 14:16
Add a ment  | 

2 Answers 2

Reset to default 7

CheckBox has only been added into React-Native in version 0.49, and only for Android. Which means that if you are developing for iOS or aren't able to upgrade your app version - you will need to use a custom checkbox ponent.

You can check out all the changes that this new version introduced at: https://github./facebook/react-native/releases/tag/v0.49.0

import React, { Component } from 'react'
import styled from 'styled-ponents'
import { TouchableOpacity, View } from 'react-native'

const CustomCheckBox = styled(View)`
  height: 24px;
  width: 24px;
  background: ${props => (props.checkBoxActive ? '#448ccb' : 'transparent')};
  border-radius: 0px;
  position: relative;
  justify-content: center;
  margin: 0px 8px 0 0;
  border: solid 1px #e1e4e5;
`
const CheckIcon = styled(View)`
  border-radius: 0px;
  align-self: center;
  transform: rotate(-30deg);
`

/*==============================
    Custom  checkbox styled 
===============================*/
const CheckIconWrapper = styled(View)`
  position: relative;
  left: 2px;
  top: -2px;
`
const CheckIconVertical = styled(View)`
  height: 5px;
  width: 2px;
  background: ${props => (props.checkBoxActive ? '#fff' : 'transparent')};
`
const CheckIconHorizontal = styled(View)`
  height: 2px;
  width: 16px;
  background: ${props => (props.checkBoxActive ? '#fff' : 'transparent')};
`

class CheckBox extends Component {
  state = {
    checkBox: false
  }
  render() {
    return (
      <TouchableOpacity
        onPress={() => {
          this.setState({ checkBox: !this.state.checkBox })
        }}>
        <CustomCheckBox checkBoxActive={this.state.checkBox}>
          <CheckIcon>
            <CheckIconWrapper>
              <CheckIconVertical checkBoxActive={this.state.checkBox} />
              <CheckIconHorizontal checkBoxActive={this.state.checkBox} />
            </CheckIconWrapper>
          </CheckIcon>
        </CustomCheckBox>
      </TouchableOpacity>
    )
  }
}

export default CheckBox

本文标签: javascriptReact Native CheckBox is not workingStack Overflow