【发布时间】:2021-04-03 02:36:26
【问题描述】:
我必须完成一些任务,即创建一个动态选择器选项。但是我在互联网上尝试了一些选项,并且大多数选项都使用类组件进行了解释。但我需要使用功能组件。创建选择器的目的是通过名称选择产品。(因此选择器在标签上注明其名称)。但在这里面,我需要使用所选产品的 ID 调用发布请求,而不是它的名称。因此,从选择器中选择选项(作为产品名称)时,我还需要获取产品 ID 作为输入。
下面是productDetails终于登陆控制台了。
{"data": [{"accountCoordinatorEmail": null, "category": "edu", "createdAt": null, "createdBy": null,
"customerEmail": "c@gmail.com", "modifiedAt": null, "modifiedBy": null, "productID": 111,
"productName": "e-School", "projectManagerEmail": null},
因此,作为示例,我需要发布 productID = 111 作为输入,同时从选择器中选择 productName 作为电子学校。我应该如何实施。我在类组件示例中看到了一些映射函数。但我无法从类组件中理解。
import React, {useEffect, useState} from 'react';
import {View, StyleSheet} from 'react-native';
import {Picker} from '@react-native-picker/picker';
import AsyncStorage from '@react-native-community/async-storage';
const ProductPicker = () => {
const [selectedValue, setSelectedValue] = useState('');
const [productDetails , setproductDetails] = useState([]);
useEffect(() => {
getProductList();
}, []);
const getProductList = async () => {
const token = await AsyncStorage.getItem('userToken');
fetch("http://10.0.2.2:3000/customer/get-all-products", {
method: "post",
headers: {
'Content-Type': 'application/json',
'Authentication': `Bearer ${token}`
},
})
.then((response) => response.json())
.then((json) => setproductDetails(json.data))
.catch((error) => console.error(error))
};
return (
<View style={styles.container}>
<Picker
selectedValue={selectedValue}
style={{height: 40, width: 150}}
onValueChange={(itemValue, itemIndex) => {
setSelectedValue(itemValue);
}}
>
</Picker>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 40,
alignItems: 'center',
},
});
export default ProductPicker;
【问题讨论】:
标签: react-native post dynamic mapping picker