Search code examples
react-nativedropdown

How to get id from json react-native-material-dropdown


I have used react-native-material-dropdown in my react native project. I am getting a data from API.

I am sorry, my English is bad.

  fetch('xxxx.json', {  
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          }
        }).then((response) => response.json())
        .then((responseJson) => {
          var count = Object.keys(responseJson.cities).length;
          for(var i=0;i<count;i++){
            //console.warn(responseJson.cities[i].name) // I need to add 
            //these names to dropdown
            this.state.drop_down_data.push({ value: responseJson.cities[i].name,id:responseJson.cities[i].id });
          }
          //this.setState({ drop_down_data });
        })
        .catch((error) => {
          console.error(error);
        });

and Dropdown code

<Dropdown
            label='City'
            data={this.state.drop_down_data}
            onChangeText={this.onCityChange.bind(this)}
          />

and Change method issue is here

onCityChange(val,ind,data){ fetch('xxxx/'+ cityid +'/towns.json', {  
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      }
    }).then((response) => response.json())
    .then((responseJson) => {
      this.setState({drop_down_data2: []});
      var count = Object.keys(responseJson.towns).length;
      for(var i=0;i<count;i++){
        //console.warn(responseJson.towns[i].name) // I need to add 
        //these names to dropdown
        //
        this.state.drop_down_data2.push({ value: responseJson.towns[i].name,id:responseJson.towns[i].id });
      }
      //this.setState({ drop_down_data });
    })
    .catch((error) => {
      console.error(error);
    });
  }

in here i want to city id.value is coming but id not coming or i dont know get to id.

if i can get id, i can a post request for town.

how i can do this?

and some json data

 {
            "id": 1,
            "name": "Adana",
            "alpha_2_code": "TR-01"
        },
        {
            "id": 2,
            "name": "Adıyaman",
            "alpha_2_code": "TR-02"
        },

Solution

  • onChangeText Method have 3 parameters (value, index, data)

    data = Complete Array

    index = Current Item index selected

    You can get id by this code

    onChangeText = (value, index, data) => {
        const cityId = data[index].id;
        console.log("cityId", cityId);
      };
    

    Complete Sample Code

    import React, { Component } from "react";
    import { Dropdown } from "react-native-material-dropdown";
    
    const data = [
      {
        value: "City1",
        id: 1
      },
      {
        value: "City2",
        id: 2
      },
      {
        value: "City3",
        id: 3
      }
    ];
    
    export default class Example extends Component {
      onChangeText = (value, index, data) => {
        const cityId = data[index].id;
        console.log("cityId", cityId);
      };
    
      render() {
        return (
          <Dropdown
            label="Favorite Fruit"
            data={data}
            style={{ marginTop: 50 }}
            onChangeText={this.onChangeText}
          />
        );
      }
    }