React Native复选框无法工作。

3

我是React Native的新手,尝试在视图中添加一个复选框,但我无法在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>
     )
  }
}

你目前使用的 RN 版本是哪个? - Wanda Ichsanul Isra
React Native 0.48 - Kumar
2个回答

7
CheckBox在React-Native的0.49版本中才被添加,而且只适用于Android。这意味着,如果您正在开发iOS应用或无法升级您的应用程序版本,则需要使用自定义复选框组件。
您可以在以下网址查看此新版本引入的所有更改:https://github.com/facebook/react-native/releases/tag/v0.49.0

-1
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接