React Native apply array values ​​from state as Picker elements

I have an array in mine state

named Categories

.

This is its meaning: ['Food', 'Home', 'Savings']

.

My goal is that I need them to appear as Picker.items

for my user.

How is this possible?

I tried to use ListView

inside an object Picker

, but when I go to this page,

AppName stops working

tips.

+6


source to share


2 answers


You don't need to use list view in select box

var options ={
    "1": "Home",
    "2": "Food",
    "3": "Car",
    "4": "Bank",
};

<Picker
    style={{your_style}}
    mode="dropdown"
    selectedValue={this.state.selected}
    onValueChange={()=>{}}>
    {Object.keys(options).map((key) => {
        return (<Picker.Item label={this.props.options[key]} value={key} key={key}/>) //if you have a bunch of keys value pair
    })}
</Picker>

      



2) When you have an array of values

var options =["Home","Savings","Car","GirlFriend"];

<Picker
    style={{your_style}}
    mode="dropdown"
    selectedValue={this.state.selected}
    onValueChange={()=>{}}> //add your function to handle picker state change
    {options.map((item, index) => {
        return (<Picker.Item label={item} value={index} key={index}/>) 
    })}
</Picker>

      

+17


source


fixed several syntax errors



{options.map((item, index) => {
   return (< Picker.Item label={item} value={index} key={index} />);
})}   

      

+6


source







All Articles