React Native 将状态中的数组值应用为 Picker 项 [英] React Native apply array values from state as Picker items

查看:33
本文介绍了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>

这篇关于React Native 将状态中的数组值应用为 Picker 项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆