简体   繁体   English

我想将JSON文件中的数据用作单选按钮的标签(本机反应)

[英]I want to use the data from a JSON file as the label for my radio button (react native)

I have an app that reads a JSON file. 我有一个读取JSON文件的应用。 In one tab I have it coming out as a list, on another tab, I want it to show the items selected from the file as the labels for a radio button I found here: 在一个选项卡中,我将其显示为列表,在另一个选项卡中,我希望它显示从文件中选择的项目,作为我在此处找到的单选按钮的标签:

https://www.npmjs.com/package/react-native-radio-buttons-group

Here's the code that I have for the tab of my app that pulls names of medications from a JSON file: 这是我的应用程序选项卡上的代码,该代码从JSON文件中提取药物名称:

import React, { Component } from "react";
import {
    View,
    Text,
    StyleSheet,
    TouchableHighlight,
    FlatList
} from "react-native";
import { Icon } from 'native-base'
import RadioGroup from 'react-native-radio-buttons-group';

//import MultipleChoice from 'react-native-multiple-choice'


class LikesTab extends Component {

  _onSelect = ( item ) => {
       console.log(item);
     };

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

  constructor(props){
    super(props);

    this.state = {
      data:[]
    }
  }

//SETTING THE STATE MAKING AN EMPTY ARRAY WHICH WE FIL
//  state = {
  //  data: []
  //};

  componentWillMount() {
    this.fetchData();
  }

//Getting the data
fetchData = async () => {
  const response = await fetch("https://api.myjson.com/bins/s5iii");
  const json = await response.json();
  this.setState({ data: json.results });
};

  //var customData = require('./customData.json');


//Setting what is shown
render() {


  return (
    <View style={{ marginVertical: 10, backgroundColor: "#E7E7E7" }} >
      <FlatList
        data={this.state.data}
        keyExtractor={(x, i) => i}
        renderItem={({ item }) =>
          <Text>
            {`${item.name.first} ${item.name.last}`}
          </Text>}

      />




    </View>
  );
}
}

export default LikesTab;



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

Here's my tab for the radio button 这是单选按钮的标签

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

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

export default class AddMediaTab extends Component {

  componentWillMount() {
    this.fetchData();
  }

//Getting the data
fetchData = async () => {
  const response = await fetch("https://api.myjson.com/bins/s5iii");
  const json = await response.json();
  this.setState({ data: json.results });
};

    state = {
        data: [
            {
                label:' ' ,
            }

        ]
    };



    // 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={styles.valueText}>
                    Value = {selectedButton}
                </Text>
                <RadioGroup radioButtons={this.state.data} onPress={this.onPress}
                 />

            </View>
        );
    }
}

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

What is your selectedButton variable? 您的selectedButton变量是什么? I think Value = {selectedButton} is the issue. 我认为Value = {selectedButton}是问题。 If selectedButton evaluates to a string, fine but it looks like it is an object in your case. 如果selectedButton求值为字符串,则可以,但是在您的情况下看起来像是一个对象。 Array.find() returns the first element that satisfies the condition, or if none satisfy it it returns undefined. Array.find()返回满足条件的第一个元素,如果不满足该条件,则返回undefined。 If that variable undefined during the time it's waiting for your api call to return something that could cause an issue as well. 如果在此时间内未定义该变量,则它将等待您的api调用返回可能也会引起问题的内容。

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

相关问题 如何在Flatlist中使用单选按钮-React Native - How to use Radio button in Flatlist - React native 我想通过本机活动传递数据以使用android中的桥接来响应本机js文件 - I want to pass data from native activity to react native js file using bridging in android 我有 4 个单选按钮。 我希望每次都选择我上次选择的单选按钮 - I have 4 radio buttons. I want that my last selected radio button get selected everytime 我想用我的按钮使用Widget.Button.Transparent样式 - I want to use Widget.Button.Transparent style with my button 我在哪里可以在我的本机项目中找到 tsconfig.json 文件 - Where can I find tsconfig.json file in my react native project 我想在 React native 中读取 Image 中的文本。 (安卓和 iOS) - I want to read text from Image in React native . (android & ios) 如何在 React-Native 中使用带有箭头功能的 Lottie Json 文件 - How to use Lottie Json file with arrow function in React-Native 如何将数据从 react native 传递到 native android 或 ios? - How to pass and use data from react native to native android or ios? 我想使用数组列出 JSON 字符串中的数据,但为什么我的应用程序崩溃了? - I want to list data from JSON string using an array, but why my app crashes? 如何从 JSON 文件在 React Native 应用程序中渲染图像? - How can I render an image in a React Native application from a JSON file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM