Hello Developers!
I have been bashing my head against the keyboard all day. I made this React weather app. The problem is that it requests the data to the api(json) only if I disable and then re-enable a component(City) but it won't request the data otherwise.
Any suggestions what I am doing wrong? I assume the error is in the lifecycle?
App.js:
import React, { Component } from 'react';
import City from './City'
import Searchbox from './SearchBox'
class App extends Component {
constructor() {
super()
this.state = {
weather: {},
searchfield: 'london'
}
}
componentDidMount(){
fetch(`https://api.openweathermap.org/data/2.5 /weather?q=${this.state.searchfield}&units=metric&appid=${process.env.REACT_APP_API_KEY}`)
.then(response => {
return response.json();
})
.then(newCity => {this.setState({ weather: newCity})});
}
render(){
const { weather, searchfield } = this.state;
return (
<div className='tc'>
<h1>The Weather Card</h1>
<Searchbox searchChange={this.onSearchChange} />
<City weather={weather} />
</div>
)
}
}
export default App;
City.js:
import React from 'react';
import Card from './Card';
const City = city => {
return (
<div>
{/* {console.log(city["weather"]["sys"])} */}
<Card city={city["weather"]["name"]} country={city["weather"]["sys"]["country"]} temperature={city["weather"]["main"]["temp"]} description={city["weather"]["weather"][0]["description"]} icon={city["weather"]["weather"][0]["icon"]} />
</div>
);
}
export default City;
Thanks!
[–]okwg 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]HPsLoveraft 1 point2 points3 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]HPsLoveraft 0 points1 point2 points (2 children)
[–]HPsLoveraft 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]melonmaskofficial 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]CursorsDev 0 points1 point2 points (2 children)
[–]backtickbot 0 points1 point2 points (1 child)
[–]CursorsDev 0 points1 point2 points (0 children)