繁体   English   中英

react-select可以加载异步数据

[英]react-select can load async data

我正在尝试使用react-select插件构建一个select组件。

在实施该项目的过程中,我遇到了一些棘手的问题。 在这里查看我的源代码: https : //codesandbox.io/s/j148r99695

  1. 我遇到的问题是我想从服务器获取所有genresList数据并将它们映射到选择组件。 但是,无论如何还是我做错了什么,这是行不通的。 请参阅上面的源代码以帮助我。

  2. 我从Movies组件中获取数据。 它工作正常,我将一个道具传递给FormFilter组件: <FormFilter genresList={this.state.genres} /> FormFilter组件中,我检查this.props.genresList ,它是否可用。 但是,当我尝试将其分配给FormFilter状态和console.log("state", this.state.genres); 那。 它是空的。 谁能告诉我为什么?

  3. 默认使用valuelabel来进行react-select以显示数据以选择组件。 但是您知道某些情况下我们必须对此进行自定义。 我通过使用map转换为其他数组来进行尝试。 但这是最好的方法吗? 如何自定义valueKeylabelKey

我正在使用react-select beta版本2。

更新:我已经修复了我的项目。 请查看下面的链接。 不知何故它不起作用。 我被推荐在源代码里面。

https://codesandbox.io/s/moym59w39p

因此,为了使它起作用,我更改了FormFilter.js实现:

import React, { Component } from "react";
import * as Animated from "react-select/lib/animated";
import AsyncSelect from "react-select/lib/Async";

class FormFilter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      inputValue: "",
      selectedOption: "",
      genres: []
    };
  }

  selectGenreHandleChange = newValue => {
    const inputValue = newValue.replace(/\W/g, "");
    this.setState({ inputValue });
    console.log(inputValue);
  };

  componentDidMount() {
    this.genresOption();
  }

  filterGenres = inputValue => {
    const genres = this.genresOption();
    //HERE - return the filter
    return genres.filter(genre =>
      genre.label.toLowerCase().includes(inputValue.toLowerCase())
    );
  };

  promiseOptions = inputValue => {
    return new Promise(resolve => { // HERE - you have to return the promise
      setTimeout(() => {
        resolve(this.filterGenres(inputValue));
      }, 1000);
    });
  };

  genresOption() {
    const options = [];
    const genres = this.props.genresList.genres; //HERE - array is genres in genresList
        if (genres && genres instanceof Array) {
          genres.map(genre => options.push({ value: genre.id, label: genre.name}));
        }
    return options;
  }

  render() {
    const { inputValue } = this.state;

    if (this.state.genres) console.log("state", this.state.genres);

    if (this.props.genresList)
      console.log("Movies props", this.props.genresList);

    return (
      <div className="filter_form">
        <span className="search_element full">
          <label htmlFor="genres">Genres</label>
          <AsyncSelect
            className="select genres"
            classNamePrefix="tmdb_select"
            isMulti
            isSearchable="true"
            isClearable="true"
            cacheOptions
            components={Animated}
            value={inputValue}
            defaultOptions
            onInputChange={this.selectGenreHandleChange}
            loadOptions={this.promiseOptions}
          />
        </span>
      </div>
    );
  }
}

export default FormFilter;

我已经写了一条评论“ HERE-something”,让您知道我的更改。 没有大问题:)

我对您的FIDDLE做了一些更改,对我有用

就像是

import React, {Component} from "react";
import { render } from 'react-dom';
import Movies from './Movies';

import "./styles.css";

class App  extends Component {
render() {
  return (
    <div className="App">
      <Movies />
    </div>
  );
}
}

let a = document.getElementById("root");
render(<App />, a);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM