React Native 将状态中的数组值应用为 Picker 项

React Native apply array values from state as Picker items

我的 state 中有一个名为 Categories 的数组。

这些是它的值:['Food', 'Home', 'Savings']

我的目标是我需要将它们呈现为 Picker.items 以便我的用户呈现 select。

这怎么可能?

我尝试在 Picker 对象中使用 ListView,但是当我导航到该页面时,

AppName stopped Working

提示。

您不需要在 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) 当你有一个值数组时

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>

更正了几个语法错误

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