admin管理员组

文章数量:1392003

I try to fill in a dropdown with data from the JSON format but for now the dropdown is empty (no results found...)

I certainly have a mistake and I can not understand where I'm confusing.

I will attach a screen of my API.

I want to get Station and NameStation..

API for Stations

My code:

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);
  }

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

I try to fill in a dropdown with data from the JSON format but for now the dropdown is empty (no results found...)

I certainly have a mistake and I can not understand where I'm confusing.

I will attach a screen of my API.

I want to get Station and NameStation..

API for Stations

My code:

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);
  }

  ponentDidMount() {
    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>
    );
  }
}
Share Improve this question edited Feb 23, 2018 at 6:39 Zlatka Pijeva asked Feb 22, 2018 at 11:25 Zlatka PijevaZlatka Pijeva 711 gold badge5 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Seems you made a typo naming the prop stations instead of options :

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

Edit : you'll need to parse your json first to pass a proper array of objects like this : [{ label: nameStation, value: Station }]

Edit 2 : Here's a parser for your data :

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

You can call this in your async request before setting the state :

.then(res => this.setState({ stations: parseStations(res.stations) }))
  1. ponentDidMount() is executed only after render() is pleted. so there's no way getStations() gets executed at the time your UI gets rendered. it is not a good idea to setState inside ponentDidMount() as it triggers re rendering. use ponentWillMount() instead.

  2. correct the typo that Dyo mentioned and use options={this.state.stations}

本文标签: javascriptHow to fill dropdown with JSON data in ReactStack Overflow