我是本机反应的新手,我正在尝试使用反应选择器显示下拉列表
我可以成功显示标签,问题是每次选择商品时,总是回到第一个选项,这里是我的代码
import React, { useState, useEffect } from 'react';
import { Text, TextInput, StyleSheet, View, Button, Picker } from 'react-native';
import NewsPost from '../hooks/NewsPost'
import CategoriesGet from '../hooks/CategoriesGet'
const CreateScreen = ({ navigation }) => {
const [category_id, setCategoryID] = useState(0)
const [categoriesGetApi, categories, errorMessageCategory] = CategoriesGet()
const [newsPostApi, errorMessage] = NewsPost()
useEffect(() => {
categoriesGetApi()
}, [])
<Picker
style={{height: 50, width:200}}
mode="dropdown"
selectedValue={category_id}
onValueChange={ itemValue => {
setCategoryID({itemValue})
console.log({itemValue})
}}
>
{
categories.map((item) => {
return(
<Picker.Item
label={item.attributes.name}
value={item.attributes.id}
key={item.attributes.id} />
)
})
}
</Picker>
我使用地图循环的类别是对象数组
[
{"attributes": {"id": 1, "name": "Sport"}, "id": "1", "type": "category"},
{"attributes": {"id": 2, "name": "Politics"}, "id": "2", "type": "category"},
{"attributes": {"id": 3, "name": "Entertainment"}, "id": "3", "type": "category"}
]
答案 0 :(得分:1)
您似乎将category_id
设置为对象{itemValue: itemValue}
,而不仅仅是值。
更改
setCategoryID({itemValue})
至
setCategoryID(itemValue)
。
我也看不到categoryName
的来源。应该是category_id
吗?