React Select - 如何在选项中显示/迭代来自api调用的数据而不是硬编码选项?

时间:2017-12-06 10:32:41

标签: reactjs react-select

我正在使用react-select,我不想硬编码应该显示的选项,但选项是我从api获取的数据。我无法真正找到任何东西,而我试图做的事情并没有显示出来。有谁知道?感谢!!!

api.js:

export function availableCities() {
   return axios.get('/cities').then(function (response) {
      return response.data;
   })
}

组件:

import Select from 'react-select';
import {availableCities} from '../utils/api.js';

class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
        selectedOption: '',
        clearable: true,
        cities: [],
     } 
   }
   componentDidMount() {
    availableCities()
        .then(res => {
            this.setState({
                cities: res.Cities.name
            })
            console.log("hello", this.state.cities)
        })
   }

   handleChange(selectedOption) {
    this.setState({selectedOption});
   }
 render(){
    let options = this.state.cities.map(function (city) {
            return city.name;
    })
 return (
      <div>
           <Select
                name="form-field-name"
                value={this.state.value}
                onChange={this.handleChange}
                clearable={this.state.clearable}
                searchable={this.state.searchable}
                options={options}                  
            />
      </div>
  )
 }
}

数据(this.state.cities是一个对象数组,如下所示:

{code: "LTS", name: "Altus", countryCode: "US", countryName: "United States", regionName: "North America", …}

...

3 个答案:

答案 0 :(得分:3)

此处的问题来自数组中的对象。 react-select需要一个包含以下键的对象数组才能理解它:valuelabel

因此,在渲染中,您可以替换

let options = this.state.cities.map(function (city) {
  return city.name;
})

,例如,

let options = this.state.cities.map(function (city) {
  return { value: city.countryCode, label: city.name };
})

或者像pritesh指出的那样,只需告诉react-select使用哪些键,如

render () {
  return (
    <div>
      <Select
        name="form-field-name"
        value={this.state.value}
        onChange={this.handleChange}
        clearable={this.state.clearable}
        searchable={this.state.searchable}
        labelKey='name'
        valueKey='countryCode'
        options={this.state.cities}                  
      />
    </div>
  )
}

希望这会帮助你!

答案 1 :(得分:0)

在您的返回组件中,您只需将从api获取的城市传递为

即可
import Select from 'react-select';
import {availableCities} from '../utils/api.js';

let isLoadingExternally = false;

class App extends React.Component {

    constructor(props) {
    super(props);
    this.state = {
        selectedOption: '',
        clearable: true,
        cities: [],
    } 
}
componentDidMount() {
    isLoadingExternally = true;
    availableCities()
        .then(res => {
            this.setState({
                cities: res.Cities.name
            }, () => {
                isLoadingExternally = false;
            })
            console.log("hello", this.state.cities)
        })
}

handleChange(selectedOption) {
    this.setState({selectedOption});
}
render(){
return (
    <div>
        <Select
                name="form-field-name"
                value={this.state.value}
                onChange={this.handleChange}
                clearable={this.state.clearable}
                searchable={this.state.searchable}
                labelKey={'name'}
                valueKey={'code'}
                isLoading={isLoadingExternally}
                options={this.state.cities}                  
            />
    </div>
)
}
}

文档中提供了许多可配置的选项。

答案 2 :(得分:0)

尝试一下:

renderList() {
 return (this.state.responseData.map(data =>({label:data.Name,value:data.value})))
}

并致电:

 <Select
    options={this.renderList()}
/>