React Native 将状态中的数组值应用为 Picker 项
问题描述:
我的 state
中有一个名为 Categories
的数组.
I have an array in my state
named Categories
.
这些是它的值:['Food', 'Home', 'Savings']
.
我的目标是我需要将它们呈现为 Picker.items
供我的用户选择.
My goal is that I need them to be rendered as Picker.items
for my user to select.
这怎么可能?
我尝试在 Picker
对象中使用 ListView
,但是当我导航到该页面时,
I tried using ListView
inside a Picker
object, but when I navigate to that page,
AppName 停止工作
AppName stopped Working
提示.
答
你不需要在 选择器
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) 当你有一个值数组时
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>