react-nativereact-native-picker-select

Get params from React native Picker selectedvalue


i fetch data from the server, and map it so i could put it in a Picker. the data is a bunch of users that have 4 attributes each (name,last name, phone, mail).on my picker i display only the name.

state = {
    value: 'first',
    selectedValue: '',
    data: [],
  };
      .
      .
      .
<Picker
                selectedValue={this.state.selectedValue}
                style={{ height: 30, width: 250 }}
                onValueChange={(itemValue, itemIndex) => this.setState({selectedValue:itemValue})} >
                {this.state.data.map((item, index) =>
                  <Picker.Item label={item.name} value={index} key={index} />
                )}
              </Picker>

My problem is i want to get all the 4 attributes(name,lname,email,phone) of the the selectedvalue so i could pass them to a new screen but i can't figure out how to do it.could someone please help show me how i could at least consol.log them


Solution

  • state = {
      value: "first",
      selectedValue: "",
      data: [],
    };
    <Picker
      selectedValue={this.state.selectedValue}
      style={{ height: 30, width: 250 }}
      onValueChange={(itemValue, itemIndex) => {
        this.setState({ selectedValue: itemValue });
        console.log(this.state.data[itemIndex]);
      }}>
      {this.state.data.map((item, index) => (
        <Picker.Item label={item.name} value={index} key={index} />
      ))}
    </Picker>;