Как получить выделенный элемент в RadioGroup в React native?

Вы можете просто использовать узкий метод ctype facet:

#include <clocale>
#include <locale>
#include <string>
#include <vector>

inline std::string narrow(std::wstring const& text)
{
    std::locale const loc("");
    wchar_t const* from = text.c_str();
    std::size_t const len = text.size();
    std::vector<char> buffer(len + 1);
    std::use_facet<std::ctype<wchar_t> >(loc).narrow(from, from + len, '_', &buffer[0]);
    return std::string(&buffer[0], &buffer[len]);
}
0
задан Vishal Patoliya ツ 17 January 2019 в 08:39
поделиться

2 ответа

Установите пакет , введя приведенную ниже команду в корневой каталог своего проекта.

npm i react-native-radio-buttons-group --save

Базовое использование

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';

import RadioGroup from 'react-native-radio-buttons-group';

export default class Vertical extends Component {
  state = {
    data: [
      {
        label: 'Default value is same as label',
      },
      {
        label: 'Value is different',
        value: "I'm not same as label",
      },
      {
        label: 'Color',
        color: 'green',
      },
      {
        disabled: true,
        label: 'Disabled',
      },
      {
        label: 'Size',
        size: 32,
      },
    ],
  };

  // update state
  onPress = data => this.setState({ data });

  render() {
    let selectedButton = this.state.data.find(e => e.selected == true);
    selectedButton = selectedButton ? selectedButton.value : this.state.data[0].label;
    return (
      <View style={styles.container}>
        <Text style={{ fontSize: 18, marginBottom: 50 }}>
          Value = {selectedButton}
        </Text>
        <RadioGroup radioButtons={this.state.data} onPress={this.onPress} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Выход

0
ответ дан Firu 17 January 2019 в 08:39
поделиться

сделать RadioGroup таким образом, давая только тег RadioGroup. поместив этот код, вы получите значение в гендерном состоянии.

<RadioGroup style={alignItems = 'flex-start'} 
    radioButtons={this.state.data} 
    onPress={data => this.setState({gender: data })} />
0
ответ дан Kazi Hasan Ali 17 January 2019 в 08:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: