New to programming in React. Using api axios i’ve retrieved weather information. What i’m trying to do is if a user select a specific city from the drop down list i want the weather information for that city to be shown – to be honest i’ve researched but not sure how to go about code.
Is there anyone can direct me. At the moment if i hard code a city i get weather on the api url i get the information but i want users to be able to choose any location from the drop down list specified.
My code so far:
import React, { useEffect } from "react";
import axios from "axios";
import "./App.css";
import { useState } from "react";
function App({ searchCities }) {
const [city, setCity] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const url = `https://api.openweathermap.org/data/2.5/weather?q=${searchCities}&units=metric&appid=08acd577f8ab8cabf637f7cd3736a629`;
useEffect(() => {
axios.get(url).then((response) => {
setCity(response.data);
console.log(response.data);
setIsLoading(false);
});
}, [searchCities]);
const [newSearchTerm, setNewSearchTerm] = useState('');
const handleChange = (event) =>{
setNewSearchTerm(event.target.value);
}
const handleSubmit = (event) => {
event.preventDefault();
searchCities(newSearchTerm);
setNewSearchTerm('');
}
if (isLoading) {
return <h2>Still Loading be patient</h2>;
}
return (
<form onSubmit={handleSubmit}>
<div>
<div>
<ul>
<select name="city">
<option value="0">Choose the City</option>
<option value="Dallas">Dallas</option>
<option value="London">London</option>
</select>
<h3>Humidity: {city.main.humidity}</h3>
<h3>Temp: {city.main.temp}</h3>
</ul>
</div>
</div>
</form>
);
}
export default App;
2
Answers
Try this
You should have an
onChange
prop to theselect
element and pass it a handler function that would update thecity
state, the handler function would be:Another thing I would suggest you is instead of using a selectable option "Choose the City" you can make it the "default" option by setting the initial value of
city
value to be"default"
and the first option would have thisvalue="default"
and pass it adisabled
prop to make it unselectable:EDIT2: I noticed you are using
setCity
for the data received, so I have created anotherselectedCity
state.The final code would look like this: