React native RNPickerSelect尝试显示从数组中对象的映射数据中选择的内容

React native RNPickerSelect尝试显示从数组中对象的映射数据中选择的内容,react-native,drop-down-menu,React Native,Drop Down Menu,我正在使用“react native picker select”库中的RNPickerSelect,但似乎无法通过映射存储在我的一个状态中的数组对象来显示选择 我尝试使用相同的方法映射数组的对象,就像我试图通过包装在简单组件中显示数据时那样,但我得到了一个“类型错误。未定义不是对象(计算this.state.selectedItem.label)” 我已经在react本机类中声明并初始化了bankResponseArray:[],并且获取数据的API调用成功,状态“bankResponseAr

我正在使用“react native picker select”库中的RNPickerSelect,但似乎无法通过映射存储在我的一个状态中的数组对象来显示选择

我尝试使用相同的方法映射数组的对象,就像我试图通过包装在简单组件中显示数据时那样,但我得到了一个“类型错误。未定义不是对象(计算this.state.selectedItem.label)”

我已经在react本机类中声明并初始化了bankResponseArray:[],并且获取数据的API调用成功,状态“bankResponseArray”已插入

[
  Object {
"__v": 0,
"_id": "5cb411e06f34961204003b79",
"bank_code": "RHB",
"country": "5cb04a7e23479e39e495f2b6",
"created_date": "2019-04-15T05:08:48.769Z",
"name": "RHB",
"status": true,
},
 Object {
"__v": 0,
"_id": "5cb42d6635ab9132e0e0b994",
"bank_code": "Maybank",
"country": "5cb04a7e23479e39e495f2b6",
"created_date": "2019-04-15T07:06:14.701Z",
"name": "Maybank",
"status": true,
},
Object {
"__v": 0,
"_id": "5cd4e8b0c4022833942eafe0",
"bank_code": "HongLeong",
"country": "5cb04a7e23479e39e495f2b6",
"created_date": "2019-05-10T02:57:52.130Z",
"name": "HongLeong",
"status": true,
},
Object {
"__v": 0,
"_id": "5cd4ee47c4022833942eafe2",
"bank_code": "testbankcode",
"country": "5cbfc9c99b7d064464592948",
"created_date": "2019-05-10T03:21:43.534Z",
"name": "testbank",
"status": true,
},, ]

下面是我的RNPickerSelect组件的外观,以及我试图从“bankResponseArray”映射数据的方式:-


[{
标签:obj.name,
值:对象id,
颜色:“rgba(77,38,22,1)”
}]
)}
onValueChange={(值,索引)=>{
这是我的国家({
bankID:value
});
}}
onClose={()=>{
//这个
}}
style={{…pickerSelectStyles}
值={this.state.businessType}
ref={el=>{
this.inputRefs.picker=el;
}}
hideIcon={Platform.OS==“ios”?false:true}
doneText={translate(“common_done”)}
//已禁用={!canSubmit}
/>

预期结果是RNPickerSelect显示4个选项,即“RHB、Maybank、HongLeong、testbank”。但目前,我尝试将数组映射到RNPickerSelect的方法是得到错误“Type error.undefined不是对象(计算this.state.selectedItem.label)。

您应该直接返回对象而不是数组

您可以这样使用:

items={this.state.bankResponseArray.map(obj => (
   {
      key: obj._id,
      label: obj.name,
      value: obj._id,
      color: "rgba(77,38,22,1)",
   }))}

map
函数中的方括号替换为圆括号i已将map函数中的方括号替换为圆括号,但仍然出现相同的错误。能否显示
map
函数的结果?map函数的结果应使RNPicker显示4个选择,其中标签为RHB、Maybank、HongLeong、 和testbank代码。它们的值作为它们的每个_id值。这就是你的意思吗?你能将
map
提取到一个单独的函数和console.log()中吗?
items={this.state.bankResponseArray.map(obj => (
   {
      key: obj._id,
      label: obj.name,
      value: obj._id,
      color: "rgba(77,38,22,1)",
   }))}