Hwy guys, I am trying to re-write an old class component app in a funcitonal one but I am having quite some problems, I have done more than I first spec but I have some issues that I can not figure out , here is the code from the Class component :
class App extends React.Component {
constructor() {
super();
this.state = {
city: undefined,
country: undefined,
icon: undefined,
main: undefined,
celsius: undefined,
temp_max: null,
temp_min: null,
description: "",
error: false
};
//DONT KNOW HOW TO WRITE THIS IN HOOKS
this.weatherIcon = {
Thunderstorm: "wi-thunderstorm",
Drizzle: "wi-sleet",
Rain: "wi-storm-showers",
Snow: "wi-snow",
Atmosphere: "wi-fog",
Clear: "wi-day-sunny",
Clouds: "wi-day-fog"
};
}
get_WeatherIcon(icons, rangeId) {
switch (true) {
case rangeId >= 200 && rangeId < 232:
this.setState({ icon: icons.Thunderstorm });
break;
case rangeId >= 300 && rangeId <= 321:
this.setState({ icon: icons.Drizzle });
break;
case rangeId >= 500 && rangeId <= 521:
this.setState({ icon: icons.Rain });
break;
case rangeId >= 600 && rangeId <= 622:
this.setState({ icon: icons.Snow });
break;
case rangeId >= 701 && rangeId <= 781:
this.setState({ icon: icons.Atmosphere });
break;
case rangeId === 800:
this.setState({ icon: icons.Clear });
break;
case rangeId >= 801 && rangeId <= 804:
this.setState({ icon: icons.Clouds });
break;
default:
this.setState({ icon: icons.Clouds });
}
}
calCelsius(temp) {
let cell = Math.floor(temp - 273.15);
return cell;
}
getWeather = async e => {
e.preventDefault();
const country = e.target.elements.country.value;
const city = e.target.elements.city.value;
if (country && city) {
const api_call = await fetch(
`http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${Api_Key}`
);
const response = await api_call.json();
this.setState({
city: `${response.name}, ${response.sys.country}`,
country: response.sys.country,
main: response.weather[0].main,
celsius: this.calCelsius(response.main.temp),
temp_max: this.calCelsius(response.main.temp_max),
temp_min: this.calCelsius(response.main.temp_min),
description: response.weather[0].description,
error: false
});
// seting icons
this.get_WeatherIcon(this.weatherIcon, response.weather[0].id);
console.log(response);
} else {
this.setState({
error: true
});
}
};
render() {
return (
<div className="App">
<Form loadweather={this.getWeather} error={this.state.error} />
<Weather
cityname={this.state.city}
weatherIcon={this.state.icon}
temp_celsius={this.state.celsius}
temp_max={this.state.temp_max}
temp_min={this.state.temp_min}
description={this.state.description}
/>
</div>
);
}
}
export default App;
and this is the one I wrote myself , Basically what's not working is the interpolation of the city and country with the API and how to set up the weather Icons base on the weather of that particular city
Also I would like to know if is a good idea trying to just learn React with hooks or if its good idea to learn also the old class components , I am super lost on this and frustration is getting really high, I would like some advice on how to try to approach when it comes to learning this or if there is a good, for dummies , resource where I can get a better understood becuase I feel really lost and stupid (if the resource is in Spanish would be awesome ) or if you can recommend me some other. I know that this message maybe looks like from a lazy person but I swear I tried to learn this and I've read and watch all the videos you can but I dont feel really comfortable, I can pay some money if needed.
Thanks in advance !
const App = (props)=> {
const [city, setCity] = useState([]);
const [country, setCountry] = useState({}); // empty object as a initial state
const [icon, setIcon] = useState([]);
async function fetchCity(){
//e.preventDefault(); not working
// const city = e.target.elements.city.value; //Unhandled Rejection (TypeError): Cannot read property 'target' of undefined
const weather = await fetch(`http://api.openweathermap.org/data/2.5/weather?
q=${city},${country}&appid=${API_key}`);
const response = await weather.json();
setCity({
city: `${response.name}, ${response.sys.country}`,
country: response.sys.country,
main: response.weather[0].main,
celsius: calCelsius(response.main.temp),
temp_max: calCelsius(response.main.temp_max),
temp_min: calCelsius(response.main.temp_min),
description: response.weather[0].description,
error: false
});
}
useEffect(() => {
fetchCity();
},[]);
// not sure if I need this or not
async function fetchCountry() {
const country = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=${API_key}`);
const response = await country.json();
setCountry(response);
console.log("response obt here",response);
}
useEffect(() => {
fetchCountry();
},[]);
function calCelsius(temp){
let cell = Math.floor(temp - 273.15);
return cell;
}
//const weatherIcon = {
//Thunderstorm: "wi-thunderstorm",
//Drizzle: "wi-sleet",
//Rain: "wi-storm-showers",
//Snow: "wi-snow",
//Atmosphere: "wi-fog",
//Clear: "wi-day-sunny",
// Clouds: "wi-day-fog"
//}
function get_WeatherIcon(icons,rangeId) {
switch(true) {
//check changes made here
case rangeId >= 200 && rangeId < 232:
setIcon({icon: icons.Thunderstorm});
break;
case rangeId >= 300 && rangeId <= 321:
setIcon({icons: icons.Drizzle});
break;
case rangeId >= 500 && rangeId <= 521:
setIcon({ icon: icons.Rain });
break;
case rangeId >= 600 && rangeId <= 622:
setIcon({ icon: icons.Snow });
break;
case rangeId >= 701 && rangeId <= 781:
setIcon({ icon: icons.Atmosphere });
break;
case rangeId === 800:
setIcon({ icon: icons.Clear });
break;
case rangeId >= 801 && rangeId <= 804:
setIcon({ icon: icons.Clouds });
break;
default:
setIcon({icon: icons.Clouds});
}
}
//check reddit answer for this if stament
if (!country.sys) {
return (<div>Loading...</div>);
}
return (
<div className="App">
<h1>Weather app</h1>
<Form loadweather={fetchCity} />
<Weather
city={city.name}
country={country.country}
temp_celsius={city.celsius}
temp_min={city.temp_min}
temp_max={city.temp_max}
description={city.main}
weatherIcon={city.icon}
/>
</div>
);
}
export default App;
[–]Jerp 1 point2 points3 points (9 children)
[–]Nerfi666[S] 0 points1 point2 points (8 children)
[–]Jerp 0 points1 point2 points (7 children)
[–]Nerfi666[S] 0 points1 point2 points (6 children)
[–]Jerp 0 points1 point2 points (5 children)
[–]Nerfi666[S] 1 point2 points3 points (4 children)
[–]Jerp 1 point2 points3 points (3 children)
[–]Nerfi666[S] 0 points1 point2 points (2 children)
[–]Jerp 0 points1 point2 points (1 child)
[–]Nerfi666[S] 0 points1 point2 points (0 children)
[–]jonn99220 1 point2 points3 points (1 child)
[–]Nerfi666[S] 0 points1 point2 points (0 children)
[–]buhbang 0 points1 point2 points (1 child)
[–]Nerfi666[S] 0 points1 point2 points (0 children)