Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native apply array values from state as Picker items

Tags:

react-native

I have an array in my state named Categories.

These are its values: ['Food', 'Home', 'Savings'].

My goal is that I need them to be rendered as Picker.items for my user to select.

How is that possible?

I tried using ListView inside a Picker object, but when I navigate to that page,

AppName stopped Working

prompts.

like image 298
John Christian Los Bañes Avatar asked Mar 25 '17 12:03

John Christian Los Bañes


2 Answers

You don't need to use listview within picker

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>
like image 181
Manjeet Singh Avatar answered Oct 22 '22 07:10

Manjeet Singh


corrected a couple of syntax errors

{options.map((item, index) => {
   return (< Picker.Item label={item} value={index} key={index} />);
})}   
like image 32
Dmitry Preeternal Avatar answered Oct 22 '22 06:10

Dmitry Preeternal