从 API 加载数据后,react-select 上的下拉列表仍然为空

Dropdown on react-select is still empty after load data from API

我在 MVC.ASP.NET 上创建了一个 API,将 returns 数据转换为 JSON 格式。 我尝试使用此 API 中的数据加载下拉列表,但下拉列表仍然是空的。

当我在下拉列表中加载静态数据时,他工作正常。

我是 React 新手,可能是我的代码有错误。

我不确定 API 数据是否正确

我只想从此 API 中获取 Station 和 StationName。

我的代码:

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

function parseStations(stations) {
  return stations.map((station) => {
    return { label: station.NameStation, value: station.Station };
  });
}

export default class Weather extends Component {
  constructor(props) {
    super(props);
    this.state = {
      options: [
        { value: true, label: 'Yes' },
        { value: false, label: 'No' },
      ], stations: [

      ],
      value: null
    }
    this.state = { stations: null}
    this.onChange = this.onChange.bind(this);
  }
  onChange(event) {
    this.setState({ value: event.value });
    console.log('Boolean Select value changed to', event.value);
  }

  componentDidMount() {
    const data =  this.getStations();
    console.log(data)
  }

  // async getStations() {
  //   fetch('http://localhost:56348/api/stations', {
  //     data: 'Station',
  //     data: 'NameStation',
  //     method: "GET"
  //   }).then(res =>
  //     console.log(res))
  //   // res.json())
  //   // .then(res => this.setState({ stations: parseStations(res.stations.Stations) }))
  //   //.then(res => this.setState({ stations: res.stations }))
  //   //.catch(e => )
  // }

  async getStations() {
    const res = await fetch('http://localhost:56348/api/stations', {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        },
    });
    return await res.json();
}

  render() {
    if (!this.state.stations) return null;
    return (
      <div className="MasterSection">
        <div className="wrapper">
          <div className="section">Изберете № на станция</div>
          <Select
            onChange={this.onChange}
            //options={this.state.options}
            options={this.state.stations}
            value={this.state.value}
            clearable={false}
          />
        </div>
        <div class="section">
          <input type="text" class="form-control" placeholder="Дата (гггг-мм-дд)"
            aria-label="Username" aria-describedby="basic-addon1"></input>
        </div>
        <div class="section">
          <input type="text" class="form-control" placeholder="Брой дни назад"
            aria-label="Username" aria-describedby="basic-addon1"></input>
        </div>
        <div class="section">
          <button type="button" class="btn btn-outline-dark">Покажи</button>
        </div>
      </div>

      // <div className="MasterSection">
      //   <div className="wrapper">
      //     <div className="section">Изберете № на станция</div>
      //     <Select
      //       onChange={this.onChange}
      //       //options={this.state.options}
      //       options={this.state.stations}
      //       value={this.state.value}
      //       clearable={false}
      //     />
      //   </div>
      //   <div class="section">
      //     <input type="text" class="form-control" placeholder="Брой дни назад" aria-label="Username" aria-describedby="basic-addon1"></input>
      //   </div>
      //   <div class="section">
      //     <button type="button" class="btn btn-outline-dark">Покажи</button>
      //   </div>
      // </div>
    );
  }
}

将您的站的初始状态设置为 null 然后,检查 render 中的状态,如果它为 null 你的下拉列表将不会呈现

constructor() {
  super();
  this.state = {
  // This should be null on initial load
   stations: null,
   ...,
  }
}

render() {
/** If state is null return null */
if(!this.state.stations) return null;
return (
  <div className="MasterSection">
    <div className="wrapper">
      <div className="section">Изберете № на станция</div>
      <Select
        onChange={this.onChange}
        //options={this.state.options}
        options={this.state.stations}
        value={this.state.value}
        clearable={false}
      />
    </div>
    <div class="section">
      <input type="text" class="form-control" placeholder="Брой дни назад" 
      aria-label="Username" aria-describedby="basic-addon1"></input>
    </div>
    <div class="section">
      <button type="button" class="btn btn-outline-dark">Покажи</button>
    </div>
  </div>
 );
}