复选框不改变反应本机元素

时间:2019-12-27 16:22:30

标签: javascript reactjs react-native

嗨,我正在做一个类似组件表单的表单,该表单在react native上带有复选框,用于标记用户是女性还是男性,但是我无法使其正常工作,它看起来和看起来都像我想要的,但我无法标记从一个选项更改为另一个选项,我也不能传递值来提交

感谢您的帮助

注意:我正在为react-native-elements组件使用库<CheckBox />

这是我的代码

import React, {  useContext,useState} from 'react';
import {
  StyleSheet,
  Text,
  View,
  TextInput,
  TouchableOpacity,
  ScrollView,
} from 'react-native';

import MyDatePicker from './MyDatePicker';
import { CheckBox } from 'react-native-elements';

const PersonalForm = ({onSubmit, errorMessage}) => {
    const [vName, setvName] = useState('');
    const [vSecondName, setvSecondName] = useState('');
    const [selectedValue, setSelectedValue] = useState(true);


return ( 
    <ScrollView>
      <View style={styles.buttonContainer}>
        <View style={styles.inputContainer}>
          <TextInput style={styles.inputs}
            placeholder="Nombre"
            underlineColorAndroid='transparent'
            onChangeText={newvName => setvName(newvName)}
            value={vName}
            autoCorrect={false}
            autoCapitalize='characters'
          />
        </View>
        <View style={styles.inputContainer}>
          <TextInput style={styles.inputs}
            placeholder="Segundo nombre"
            underlineColorAndroid='transparent'
            onChangeText={newvSecondName => setvSecondName(newvSecondName)}
            value={vSecondName}
            autoCorrect={false}
            autoCapitalize='characters'
          />
          </View>
          </View>
          <CheckBox
            containerStyle={styles.checkbox}
            textStyle={styles.checkboxTxt}
            uncheckedColor={'#b3b4b5'}
            checkedColor={"#911830"}
            key={4}
            title="Male"
            value={4}
            value="4"
            checkedIcon="stop"
            checked={selectedValue}
            onChange={()=>setSelectedValue(true)}
          />
          <CheckBox
            containerStyle={styles.checkbox}
            textStyle={styles.checkboxTxt}
            uncheckedColor={'#b3b4b5'}
            checkedColor={"#911830"}
            key={3}
            title="Female"
            value={3}
            value="3"
            checkedIcon="stop"
            checked={!selectedValue}
            onChange={()=>setSelectedValue(false)}
          />
          <View>
              <MyDatePicker />
          </View>
              <View style={styles.buttonContainer2}>
              <TouchableOpacity 
                style={ styles.logout}  
                onPress={() => onSubmit(vName, vSecondName, vGender, vEmail)}
              >
                  <Text style={styles.loginText}>GUARDAR</Text>
              </TouchableOpacity>
          </View>
        </ScrollView>
    );
};

1 个答案:

答案 0 :(得分:3)

您使用的回调错误。如docs中所述,您要查找的回调是onPress而不是onChange

将其更改为此:

 <CheckBox
        containerStyle={styles.checkbox}
        textStyle={styles.checkboxTxt}
        uncheckedColor={'#b3b4b5'}
        checkedColor={"#911830"}
        key={4}
        title="Male"
        value={4}
        value="4"
        checkedIcon="stop"
        checked={selectedValue}
        onPress={()=>setSelectedValue(true)}
      />
     <CheckBox
        containerStyle={styles.checkbox}
        textStyle={styles.checkboxTxt}
        uncheckedColor={'#b3b4b5'}
        checkedColor={"#911830"}
        key={3}
        title="Female"
        value={3}
        value="3"
        checkedIcon="stop"
        checked={!selectedValue}
        onPress={()=>setSelectedValue(false)}
      />