0

im trying to display weather icons from open weather map api , but am not entirely sure how to do it , here is the documentation https://openweathermap.org/weather-conditions .. im passing inthe image url just like its written in the docs but im just getting a broken image instead ,can someone please tell me what i am doing wrong? thanks

App,js

class App extends React.Component {
  state = {
    temperature: "",
    city: "",
    country: "",
    pressure: "",
    humidity: "",
    description: "",
    rain:"",
    main:"Drizzle",
    icon: "09d",
    error: ""
  }

  handlenum1Change (evt) {



let temp = (evt.target.value);




}


  getWeather = async (e) => {
    e.preventDefault();
    const city = e.target.city.value;

    const api_call = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`);
    const data = await api_call.json();

    if (city) {
      this.setState({
        temperature: data.main.temp,
        city: data.name,
        main: data.main,
        data.weather[0].icon,
        rain: data.rain,
        pressure: data.main.pressure,
        humidity: data.main.humidity,
        description: data.weather[0].description,
        error: ""
      });
    } else {
      this.setState({
        temperature: undefined,
        city: undefined,
        country: undefined,
        humidity: undefined,
        description: undefined,
        pressure:undefined,
        rain : undefined,
        error: "Please enter the values."
      });
    }
  }




  render() {
    return (
      <div>
        <div className="wrapper">
          <div className="main">
            <div className="container">
              <div className="row">
                <div className="col-xs-5 title-container">


                </div>
                <div className="col-xs-7 form-container">
                  <form onSubmit={this.getWeather} >

                  <input type="text" name="city" onChange={this.handlenum1Change} placeholder="City..."/>

    <button>Get Weather</button>


    </form>




                  <Weather 
                    temperature={this.state.temperature} 
                    humidity={this.state.humidity}
                    city={this.state.city}
                       pressure={this.state.pressure}
                    description={this.state.description}
                    rain={this.state.rain}
                    icon={this.state.icon}
                    error={this.state.error}
                  />
                </div>

              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }
};

export default App;

weather.js

import React from 'react';
import PropTypes from 'prop-types';

const Weather = props =>
  <div>
    <p>{props.city}</p> 

        <p> humidity {props.humidity }</p> 

        <p> {props.description} </p>
    <p> temperature {props.temperature}</p> 
     <p> atmospheric pressure : {props.pressure}</p> 
 <p> atmospheric pressure : {props.rain}</p> 


 <img className="img-fluid" src={'http://openweathermap.org/img/w/${props.icon}.png'}/>
  </div>





export default Weather; 
Leo Bogod
  • 369
  • 8
  • 28

2 Answers2

0

If you call the image like this <img className="img-fluid" src="openweathermap.org/img/w/" + {props.icon} + ".png"/> and you still see an error, that probably means you're trying to view the image before props.icon is available--before the api call returns the data.

Add something like loading: true to your state. Then add something like this to getWeather:

fetch("http://api.openweathermap.org/data/2.5/weather?q=${this.state.city}&appid=${API_KEY}&units=metric")
.then(data => data.json())
.then(data => this.setState({ loading: false })); 

(You'll also want to use data in the above line to set all of the appropriate values like you're already doing.)

Update handlenum1Change to actually grab the value of the city that's entered:

this.setState({ city: evt.target.value });

Then wrap the Weather component:

{ this.state.loading ? '' : <Weather/> }

With the above line nothing should display until the data is returned from fetch.

Pat Mellon
  • 360
  • 2
  • 5
  • 12
0

You have to use the URL, instead of just the icon value. In this case, you can use:

http://openweathermap.org/img/w/${this.state.icon}.png

The final code should be:

<img src = {'http://openweathermap.org/img/w/${this.state.icon}.png'}/>
kyun
  • 9,710
  • 9
  • 31
  • 66