2

I created an API on MVC.ASP.NET that returns data to a JSON format. I try to load a dropdown with data from this API but dropdown is still empty.

When I load a static data in dropdown, He work fine.

Im new in react, and may be the mistake is in my code.

I'm not sure I'm getting the API data right

I want to take only Station and StationName from this API. API

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

1 Answer 1

2

Set your station's initial state to null Then, check the state in render if it's null your dropdown won't render

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>
 );
}
Sign up to request clarification or add additional context in comments.

7 Comments

How can initial state to null ? Stations[ { value: null } ] ?
this.state = { stations: null, }
I update my code but dropdown is still empty :( maybe I do some wrong ?
log your state in render and check if it has some data? console.log(this.state.stations)
@ZlatkaPijeva this wouldn't make any difference. Don't accept as answer unless it solved your problem. what's the difference if the state is null or [] empty array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.