尽管单击其他选项,但无法更改React本机选择器

时间:2020-02-07 16:03:15

标签: reactjs react-native

我是本机反应的新手,我正在尝试使用反应选择器显示下拉列表

我可以成功显示标签,问题是每次选择商品时,总是回到第一个选项,这里是我的代码

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"}
]

1 个答案:

答案 0 :(得分:1)

您似乎将category_id设置为对象{itemValue: itemValue},而不仅仅是值。

更改 setCategoryID({itemValue})setCategoryID(itemValue)

我也看不到categoryName的来源。应该是category_id吗?