简体   繁体   English

如何从 Reac Native 中的单选按钮一次只选择 select 一个选项

[英]How to only select one option at a time from radio button in Reac Native

Im using galio's radio component this here .在这里使用 galio 的无线电组件。 Im trying to select only one value at a time.我试图 select 一次只有一个值。 I try to achieve that with this code:我尝试使用以下代码实现这一目标:

const [selected, setSelected ] = useState(false);
const OptionList = (groupOption) => {
   return (
     <FlatList
       data={groupOption.options}
       keyExtractor={(result) => result.id.toString()}
       renderItem={({ item }) => {
         return (
           <View>
             <Radio label={item.description} onChange={() => setSelected(true)} initialValue={selected} />
           </View>
         );
       }}
     />
   );
 };

But since its in a flat list it changes the value for all of them.但由于它在一个平面列表中,它改变了所有这些的值。 How can i only select one radio button at a time?我怎样才能一次只 select 一个单选按钮? Any ideas?有任何想法吗?

Use Radio Group or save the selected index in the state, when the user clicks on the radio button, change that selected index and mark it selected.使用 Radio Group 或将所选索引保存在 state 中,当用户单击单选按钮时,更改所选索引并将其标记为选中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM