【问题标题】:React-Select dropdown is not displaying options of array or value in inputReact-Select 下拉菜单未在输入中显示数组或值的选项
【发布时间】:2018-11-13 21:30:06
【问题描述】:

所以我遇到了这个问题,组件加载得很好,出现了一个下拉菜单,但是它没有显示任何标签或值。当我单击该选项时,它会显示一个选中的框,但框中没有任何内容。请看下面的代码。我正在使用最新的 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>);
    }
}

【问题讨论】:

    标签: javascript reactjs react-select


    【解决方案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

    【讨论】:

    • 我刚回到帖子向自己指出这一点!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-12-29
    • 2021-03-07
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    相关资源
    最近更新 更多