21

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.

2 Answers 2

53

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Hello Sir, Thank You for the solution, It worked, but it alerted warnings. It says 'React.createElement: type is invalid' Sir where FROM will I import the Item component?
Never mind, I just changed the <Item/> to <Picker.Item />. The alerts were gone after that. Thank you again Kind Sir.
Hi bro, I am not 'SIR' just a fellow programmer like you.You can replace Item with Picker.Item or check facebook.github.io/react-native/docs/picker.html for example
@ManjeetSingh when tried like {options.map((item, index) => { return (<Picker.Item label={item} value={index} key={index}/>) })} , I am getting the error - Invalid prop label` of type object supplied to PickerItem, expected string.`
If I want to add an option e.g "Please Select" so what will I do for it?
|
11

corrected a couple of syntax errors

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.