【问题标题】:MultiSelect in react nativeMultiSelect 在本机反应
【发布时间】:2018-03-20 15:58:32
【问题描述】:

我正在尝试在本机反应中实现 MultiSelect。我已经从这个链接“https://github.com/toystars/react-native-multiple-select”中提到了。但不幸的是我 无法在显示“没有要显示的项目”的下拉列表中查看名称。

图片:

对于要在下拉列表中显示的名称,数据取自 items 属性,该属性应为 javascript 对象数组的形式。请帮我解决这个问题。

import React, {Component} from 'react';
import { SectionList, Image, StyleSheet, Text, View, ScrollView, ListView, 
AsyncStorage, Button, TextInput, TouchableOpacity, KeyboardAvoidingView  } 
from 'react-native';
import { Constants } from 'expo';
import ActionButton from 'react-native-action-button';
import Icon from 'react-native-vector-icons/Ionicons';
import { StackNavigator } from 'react-navigation';
import { Ionicons } from '@expo/vector-icons';
import TextField from 'react-native-md-textinput';
import MultiSelect from 'react-native-multiple-select'; 

export default class SendNotification extends Component {

static navigationOptions = {
title: 'Send Notification',
};

constructor (props) {
super(props)
this.state = {
arr_user: [],           
} 
}

componentWillMount() {
this.getUsers();
};

getUsers = async () => {
const { page, seed } = this.state;
await fetch('.....api') 
  .then((response) => response.json()) 
  .then((responseJson) => { 
      
   this.setState({arr_user: responseJson.results});

 }).catch((error) => { console.error(error); });
  
   
};

focus () {
this.textInput && this.textInput.focus()
};  

onSelectedItemsChange = (selectedItems) => {

console.log(JSON.stringify(selectedItems));
this.setState({selected_user: JSON.stringify(selectedItems)});
};

render() {

return (
  
  <View style={{flex:1, backgroundColor:'#ffffff'}}>

    <ScrollView>
    
      <MultiSelect
        items={this.state.arr_user}
        uniqueKey="id"
        onSelectedItemsChange={this.onSelectedItemsChange}
        selectedItems={[]}
        selectText="Pick Users"
        searchInputPlaceholderText="Search Users..."
        tagRemoveIconColor="#CCC"
        tagBorderColor="#CCC"
        tagTextColor="#CCC"
        selectedItemTextColor="#CCC"
        selectedItemIconColor="#CCC"
        itemTextColor="#000"
        searchInputStyle={{ color: '#CCC' }}
        submitButtonColor="#CCC"
        submitButtonText="Submit"
      />

      </ScrollView>
  </View>

 );
} 
}

【问题讨论】:

    标签: reactjs react-native dropdown multi-select


    【解决方案1】:

    您不是将 async / await 与“经典”(获取)承诺语法混为一谈吗?所以不要写

    await fetch(YOUR_API) 
    .then((response) => response.json()) 
    .then((responseJson) => ...
    

    你应该写

    ... async () => {
       const  { page, seed } = this.state;
          try {
             const response = await fetch('..//api');
             const responseJson = await response.json();
             [DO CONDITIONAL CHECKS IF NEEDED]
             this.setState({ arr_user: responseJson });
          } catch (e) {
             console.log(e);
          }
    }
    

    否则你可以用更经典的方式编写你的 fetch() 但没有“async/await” 这篇文章对我来说是对 async/await 的一些有用的介绍:6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

    【讨论】:

    • 我试过了,但还是不行……还是同样的问题“没有要显示的项目”
    • 您是否调试过您的responseJson,也许该响应被包装在另一个对象中?自己没用过那个组件,以后试试。
    • 其实我是从 API 获取 JSON 格式的数据,我的数据有点像这样 [{"id":"14","name":"Ondo"},{"id ":"15","name":"Ogun"},{"id":"16","name": "Calabar"}]
    • 更正:实际上我是从 API 获取 JSON 格式的数据,我的数据有点像这样 {"results":[{"id":"14","name":" Ondo"},{"id":"15","name":"Ogun"},{"id":"16","name":"Calabar"}]} 我不知道我做错了什么? !!
    • @AniketParab 你确定这不是拼写错误吗?根据上面的评论,您的 API 响应带有 results,而您在下面粘贴的代码中访问 result
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2020-08-10
    • 2016-04-18
    • 2021-12-26
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多