React-Select下拉列表未在输入中显示数组或值的选项

时间:2018-11-13 21:30:06

标签: javascript reactjs react-select

因此,我遇到了这个问题,组件加载正常,出现一个下拉列表,但是没有标签或值。当我单击该选项时,它显示一个选定的框,但框内没有任何内容。请参见下面的代码。我将最新的React-Select用于多选输入,但是找不到有关如何为多选构建数组的任何文档。

import React, { Component } from 'react';
import '../index.css';
import Select from "react-select";

export class Invite extends Component {

    constructor(props) {
        super(props);

        this.state = {
            multiValue: [],
            filterOptions: []
        };

        this.handleMultiChange = this.handleMultiChange.bind(this);
    }

    componentDidMount = () => {
        let userList = [];
        let list = [];
        list = this.props.hub.users;
        for (var i = 0; i < list.length; i++) {
            userList.push([{label: list[i].userName, value: list[i].userName }]);
        }
        this.setState({ filterOptions: userList });
    }

    handleMultiChange(option) {
        this.setState(state => {
            return {
                multiValue: option
            };
        });
    }

    render() {

        return (<div className="invite">
            <Select
                name="Users"
                placeholder="Users"
                value={this.state.multiValue}
                options={this.state.filterOptions}
                onChange={this.handleMultiChange.bind(this)}
                isMulti
            />
        </div>);
    }
}

1 个答案:

答案 0 :(得分:1)

您在componentDidMount中犯了错误userList应该是对象数组而不是数组。

componentDidMount = () => {
  let userList = [];
  let list = [];
  list = this.props.hub.users;
  for (var i = 0; i < list.length; i++) {
    userList.push({
      label: list[i].userName,
      value: list[i].userName
    });
  }
  this.setState({
    filterOptions: userList
  });
}

有关更多详细信息,请访问documentation