简体   繁体   English

如何在React Native中使用radioForm呈现选中/选定的单选按钮

[英]How to render checked/selected radio button using radioForm in react native

I am new to React-native. 我是React-native的新手。 I have three values for radio button "Male","Female","other" with respective values "141","142","143". 对于单选按钮“ Male”,“ Female”,“ other”,我有三个值,分别具有值“ 141”,“ 142”,“ 143”。 Initially "Male" is selected but if we select "female/Other" still it is showing "Male" as selected, though value is being saved. 最初选择“男性”,但是如果我们选择“女性/其他”,尽管值被保存,但仍显示为“男性”。 My code snippet is mentioned below: 我的代码段如下所述:

    class advanced_targeting extends Component {
       static navigationOptions = {
          title: 'Advanced Targeting',
       };
      constructor() {
          super(props)
         this.state = {
           isLoading: true,
           radio_props: [{ label: 'Male', value: 141 }, { label: 'Female', value: 142 }, { label: 'Other', value: 143 }],
         }
       }
       render() {
          return (
           <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
              <View style={styles.MainContainerViewCamp}>
                <Text style={{ padding: 5, fontSize: 35, backgroundColor: '#2196F3', marginBottom: 7 }}> Advanced Targeting</Text>
                  <ScrollView keyboardShouldPersistTaps='always'>
                     <RadioForm
                        ref="radioForm"
                        style={styles.radioButtonStyle}
                        radio_props={this.state.radio_props}
                        isSelected = {true}
                        initial={this.state.value}
                        formHorizontal={true}
                        buttonColor={'#2196f3'}
                        labelStyle={{ marginRight: 20 }}
                        animation={true}
                        onPress={(value,index) => {
                          this.setState({
                             value: value,
                             valueIndex: index
                          })
                        }} />
               <TouchableOpacity
                   style={styles.SubmitButtonStyle}
                   activeOpacity={.5}
                   onPress={this.saveAdvancedDemography}
               >
                 <Text style={styles.TextStyle}> SAVE DETAILS </Text>
               </TouchableOpacity>
            </ScrollView>
         </View>
      </TouchableWithoutFeedback>
    );
  }
   saveAdvancedDemography = () => {
        const base64 = require('base-64');
       fetch('APIURL', {
           method: 'POST',
           headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
              "Authorization": "Basic " + base64.encode("demo:ABCD")
           },
        body: JSON.stringify(
        {

          "dmp_sex":
            {
              "main": this.state.value,
              "data_type": "STRING"
            },
        })
    })
  }

In main, This.state.value is not getting updated. 总的来说,This.state.value不会被更新。 Please, Help 请帮忙

You don't have value and valueIndex in your component's state. 您的组件状态中没有valuevalueIndex Ideally, you should put genderValue in your component's state that would change its value upon choosing an option. 理想情况下,应将性别值置于组件的状态,该状态将在选择选项时更改其值。 Your state should look like: 您的状态应如下所示:

constructor() {
  super(props)
  this.state = {
    genderValue: 141,
    isLoading: true,
    radio_props: [{ label: 'Male', value: 141 }, { label: 'Female', value: 142 }, { label: 'Other', value: 143 }],
  }
}

And in your RadioForm: 在您的RadioForm中:

<RadioForm
  ...
  initial={ this.state.genderValue }
  onPress={(value) => {
    this.setState({
      genderValue: value,
    })
  }} />

Then you can use this.state.genderValue in saveAdvancedDemography() . 然后,您可以在saveAdvancedDemography()中使用this.state.genderValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置选中的单选按钮状态。 不使用广播组 - How to set checked radio button state . not using radio group 如何在Flatlist中使用单选按钮-React Native - How to use Radio button in Flatlist - React native 如何选中单选按钮以及如何仅控制在列表视图中选择的一个单选按钮 - How to get checked radio button and how to control only one radio button selected in list view 在本机反应中对Radioform进行索引明智的Setstate onpress功能 - Index wise Setstate onpress funtion for Radioform in react native 如何使用本机反应检测复选框是否选中? - How to detect checkbox checked or not using react native? Android-如果选中了单选按钮,如何启用按钮? - Android - How to enable a button if a radio button is checked? 如何使用共享首选项保存在微调器上和从选中的单选按钮中选择的数据 - How to use Shared Preferences to save data selected on Spinner and from checked Radio Button 检查是否使用共享首选项选中单选按钮 - check if radio button is checked using shared preferences 如何在列表视图中检查单选按钮? - How to get checked radio button in list view? 如何保存在方向更改时选中的单选按钮? - How to save radio button checked on orientation change?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM