javascript - 如何在 React 中用 JSON 数据填充下拉列表?

标签 javascript reactjs api fetch-api

我尝试用 JSON 格式的数据填充下拉列表,但现在下拉列表是空的(未找到结果...)

我当然有错误,我不明白我在哪里感到困惑。

我将附上我的 API 的屏幕。

我想要 Station 和 NameStation..

API for Stations

我的代码:

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.onChange = this.onChange.bind(this);
  }
  onChange(event) {
    this.setState({ value: event.value });
    console.log('Boolean Select value changed to', event.value);
  }

  componentDidMount() {
    this.getStations();
  }

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

  render() {
    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>
    );
  }
}

最佳答案

似乎你在命名 Prop stations 而不是 options 时打错了字:

<Select
    onChange={this.onChange}
    options={this.state.stations} // here
    value={this.state.value}
    clearable={false}
/>

编辑:您需要首先解析您的 json 以传递适当的对象数组,如下所示:[{ label: nameStation, value: Station }]

编辑 2:这是您的数据的解析器:

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

您可以在设置状态之前在异步请求中调用它:

.then(res => this.setState({ stations: parseStations(res.stations) }))

关于javascript - 如何在 React 中用 JSON 数据填充下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48926269/

相关文章:

javascript - 为什么要使用哈希 CSS 样式表和 Javascript 文件名?

javascript - 如何验证十进制数

html - 无法覆盖 Tailwind 中的线性渐变吗?

api - 强制 docusign 对客户端服务器进行 API 调用

c++ - 使用 IPC 共享内存的应用程序可以访问彼此的代码吗?

javascript - 以编程方式 React Router v4 导航

javascript - Chrome 的 Openlayers 3 "rendering hardware-accelerated"禁用个人磁贴

reactjs - 如何让 VSCode 与 React 语法一起玩得更好?

javascript - 向导入的组件添加样式

api - RSS 提要搜索引擎