【发布时间】:2018-09-17 12:45:26
【问题描述】:
我正在尝试构建一个简单的应用程序,该应用程序将显示一个下拉菜单,该下拉菜单的值是从 fetch 的返回中获得的。此外,我只想在下拉/选择器/或我可以选择 1 个选项的任何其他组件中显示该位置。
我应该把返回值放在一个数组中还是什么?
当我安慰返回时,是这样的
[
{
"ID": "BOGOR~10~522",
"Location": "BOGOR"
},
{
"ID": "JADETABEK~16~502",
"Location": "JADETABEK"
}
]
这里是测试类
import React , { Component } from 'react';
import { View , StyleSheet , Text , Dimensions , Picker } from 'react-native';
import { Dropdown } from 'react-native-material-dropdown';
import { Item } from 'native-base';
const ScreenWidth = Dimensions.get('window').width;
const Screenheight = Dimensions.get('window').height;
export default class testing extends Component {
constructor(props) {
super(props)
this.state = {
data: []
}
}
componentDidMount() {
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({"lokasi":
{
}
})
})
.then(response => response.json())
.then(res => {
console.log(res.PilihLokasiResult.Lokasi)
this.setState({
data: res.PilihLokasiResult.lokasi
})
})
}
render() {
return(
<View style={styles.container}>
{this.state.data.map((index) => {
return(
<Dropdown/>
)
})}
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
})
所以现在构造函数中的 data[] 具有响应的值,就像第一个框中一样。但我不知道如何将位置值设置为下拉项。谁能帮忙?谢谢
【问题讨论】:
-
github.com/n4kz/react-native-material-dropdown 它等待
data属性作为数组。因此,您需要为选项传递一个数组。您可以从获取结果中创建此数组。我无法理解您与“位置”相关的另一个问题。如果您能更清楚地解释它,也许有人可以提供帮助。 -
当我调用 fetch 方法时,它会返回一个结果,其中 res.PilihLokasi.Lokasi 的值就像上面的第一个框。所以我需要返回的位置值,放入下拉项。
-
我做对了吗?你想要一个包含
Location值的数组来传递Dropdown组件吗? -
是的,你是对的.. 所以我希望下拉列表中有项目:-)BOGOR -)JADETABEK。我是 react native 的初学者,所以如果我做错了请帮忙
-
没有错,你需要获取想要的数据并传递给组件。我已经提供了一个答案,我希望它有效。
标签: reactjs react-native