How to get JSON dynamic value in Picker React-native

后端 未结 2 1798
清酒与你
清酒与你 2020-12-20 01:49

Here am trying to get value in Picker , I am getting value in JSON format and trying to show that value in Picker like DropDown . Please help. Below code am trying like hit

相关标签:
2条回答
  • 2020-12-20 02:10
    import * as React from 'react';
    import { Text, View, StyleSheet, Alert, Picker } from 'react-native';
    import { Constants } from 'react-native';
    
    export default class AddInventory extends React.Component {
    
      // add a selectValue to your state to stop the overwriting
      state = {
        PickerValueHolder: [],
        selectedValue: ''
      }
    
      componentDidMount() {
        // remove the return 
       fetch('http:///Dsenze/userapi/inventory/viewinventorytype', {  
            method: 'POST',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              "username" :"admin",
              "password" :"admin"
            })
          }).then((response) => response.json())
          .then((responseJson) => {
            // use the inventoryTypeData as it is already an array
            let PickerValueHolder = responseJson.inventoryTypeData;
            this.setState({ PickerValueHolder }); // Set the new state
          })
          .catch((error) => {
            console.error(error);
          });
        }
    
      GetPickerSelectedItemValue=()=>{
        Alert.alert(this.state.PickerValueHolder);
      }
    
      render() {
    
    
        return (
          <View style={styles.container}>
            {<Picker
                    selectedValue={this.state.selectedValue}
                    onValueChange={(itemValue, itemIndex) => this.setState({selectedValue: itemValue})} >
                    { this.state.PickerValueHolder.map((item, key)=>
                      <Picker.Item label={item.name} value={item.name} key={key} />
                    )}
                  </Picker>}
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        backgroundColor: '#ecf0f1',
        padding: 8,
      }
    });
    
    0 讨论(0)
  • 2020-12-20 02:14

    You've got a few issues in your code

    1. You don't need a return statement in your componentDidMount
    2. You're not accessing the values correctly in your responseJson, there is no key called messages
    3. You are using a for-loop when you could just use the array from the responseJson.
    4. In your picker you are overwriting the PickerValueHolder which holds all the values each time you move the picker.

    I have made some changes to your code, and this is a working example, which you can see here at this snack https://snack.expo.io/@andypandy/picker-example

    import * as React from 'react';
    import { Text, View, StyleSheet, Alert, Picker } from 'react-native';
    import { Constants } from 'expo';
    
    export default class App extends React.Component {
    
      // add a selectValue to your state to stop the overwriting
      state = {
        PickerValueHolder: [],
        selectedValue: ''
      }
    
      componentDidMount() {
        // remove the return 
       fetch('http://1/Dsenze/userapi/inventory/viewinventorytype', {  
            method: 'POST',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              "username" :"admin",
              "password" :"admin"
            })
          }).then((response) => response.json())
          .then((responseJson) => {
            // use the inventoryTypeData as it is already an array
            let PickerValueHolder = responseJson.inventoryTypeData;
            this.setState({ PickerValueHolder }); // Set the new state
          })
          .catch((error) => {
            console.error(error);
          });
        }
    
      GetPickerSelectedItemValue=()=>{
        Alert.alert(this.state.PickerValueHolder);
      }
    
      render() {
    
    
        return (
          <View style={styles.container}>
            {<Picker
                    selectedValue={this.state.selectedValue}
                    onValueChange={(itemValue, itemIndex) => this.setState({selectedValue: itemValue})} >
                    { this.state.PickerValueHolder.map((item, key)=>
                      <Picker.Item label={item.name} value={item.name} key={key} />
                    )}
                  </Picker>}
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        paddingTop: Constants.statusBarHeight,
        backgroundColor: '#ecf0f1',
        padding: 8,
      }
    });
    
    0 讨论(0)
提交回复
热议问题