React Native CheckBox无法正常工作

时间:2017-10-10 14:02:53

标签: javascript reactjs react-native

我是新手来回应原生并尝试在视图中添加一个复选框,但我无法在本机反应视图中获得复选框。

提前致谢。

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>
     )
  }
}

2 个答案:

答案 0 :(得分:6)

CheckBox仅在版本0.49 中添加到React-Native中,而仅用于Android 。这意味着如果您正在为iOS开发或无法升级您的应用版本 - 您将需要使用自定义复选框组件。

您可以查看此新版本引入的所有更改:https://github.com/facebook/react-native/releases/tag/v0.49.0

答案 1 :(得分:0)

import React, { Component } from 'react'
import styled from 'styled-components'
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