将Json响应加载到本机响应中的下拉列表中

unk*_*123 2 javascript react-native react-native-android react-native-ios

我在我的应用程序中使用了材料下拉列表

<Dropdown 
  baseColor='white'
  itemColor='white'
  label='Select Cluster'
/>
Run Code Online (Sandbox Code Playgroud)

我像这样获取JSON对象,它工作正常.

  fetch('url', {  
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      username : "admin"
    })
  }).then((response) => response.json())
  .then((responseJson) => {
    var count = Object.keys(responseJson.message.Obj).length;
    for(var i=0;i<count;i++){
      console.log(responseJson.message.Obj[i].name) // I need to add 
      //these names to dropdown
    }
  })
  .catch((error) => {
    console.error(error);
  });
Run Code Online (Sandbox Code Playgroud)

现在我需要将responseJson.message.Obj[i].name值添加到我的下拉列表中.

Thé*_*dvn 10

假设您正在使用react-native-material-dropdown.

例:

Dropdown 零件:

<Dropdown
  baseColor='white'
  itemColor='white'
  label='Select Cluster'
  data={this.state.drop_down_data} // initialise it to []
/>
Run Code Online (Sandbox Code Playgroud)

请求代码:

fetch('url', {
  ...
}).then((response) => response.json())
.then((responseJson) => {
  var count = Object.keys(responseJson.message.Obj).length;
  let drop_down_data = [];
  for(var i=0;i<count;i++){
    console.log(responseJson.message.Obj[i].name) // I need to add 
    drop_down_data.push({ value: responseJson.message.Obj[i].name }); // Create your array of data
  }
  this.setState({ drop_down_data }); // Set the new state
})
.catch((error) => {
  console.error(error);
});
Run Code Online (Sandbox Code Playgroud)

文档: