Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

How to change value only from radio button group in react native

everyone. Thanks for your time. This is supossed to be very simple, but I am having trouble dealing with.

I have a "survey" which contains a few questions and 3 radio buttons for each question. The problem is: when I select an option from any radio button, the same option is selected for every question.

I just need to change the value from this group only. I am using RadioButton from react-native-paper. The RadioButton.Group is supposed to deal with this but I am getting to nowhere. How can I do this? I saw an example using react-native-simple-paper but it uses Classes and I using a Function component, so I can't use setState like almost every other library does.

Here's my code sample:

const [value, setValue] = React.useState('');

<ScrollView style={{ flexDirection: 'column' }}>
        {questions &&
          questions .map((p) => {
            return (
              <View style={{
                //flexDirection: 'column',
                elevation: 1,
                borderRadius: 5,
                backgroundColor: '#fff',
                marginTop: 15,
                marginLeft: 15,
                marginRight: 15,
                padding: 10
              }}>
                <Text
                  style={{
                    color: '#16416e',
                    fontFamily: 'Khula-Bold',
                    textTransform: 'capitalize',
                    fontSize: 20
                  }}>
                  {myCount++ + ' - ' + p.QUESTION_TEXT}
                </Text>
                <View style={{ marginTop: 15 }}>
                  <RadioButton.Group onValueChange={newValue => setValue(newValue)} value={value} key={myCount - 1}>
                    <View style={{ flexDirection: 'row' }}>
                      <RadioButton key={"S" + myCount - 1} color='#1b2f75' uncheckedColor='#1b2f75' value="S" />
                      <Text onPress={() => { setValue("S") }} style={{ fontFamily: 'Khula-Bold', color: '#16416e', fontSize: 20, marginTop: 5, marginLeft: 5, paddingBottom: 15 }} >Sim</Text>
                    </View>
                    <View style={{ flexDirection: 'row' }}>
                      <RadioButton key={"N" + myCount - 1} color='#1b2f75' uncheckedColor='#1b2f75' value="N" />
                      <Text onPress={() => { setValue("N") }} style={{ fontFamily: 'Khula-Bold', color: '#16416e', fontSize: 20, marginTop: 5, marginLeft: 5, paddingBottom: 15 }}>N?o</Text>
                    </View>
                    <View style={{ flexDirection: 'row' }}>
                      <RadioButton key={"NA" + myCount - 1} color='#1b2f75' uncheckedColor='#1b2f75' value="NA" />
                      <Text onPress={() => { setValue("NA") }} style={{ fontFamily: 'Khula-Bold', color: '#16416e', fontSize: 20, marginTop: 5, marginLeft: 5, paddingBottom: 15 }}>N/A</Text>
                    </View>
                  </RadioButton.Group>
                </View>
              </View>
            );
          })}
      </ScrollView>
question from:https://stackoverflow.com/questions/65945113/how-to-change-value-only-from-radio-button-group-in-react-native

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...