I would like to ask some help. I am trying to send the whole input value as a whole to the fetch URL by passing it to useState
variable. My own code seems work but every time I starting to type, the key is automatically passed to the useState
variable then the app crashed since there is no city with just 1 letter. How to handle this? The thing I want is after I send enter key, that's the time the value of input will e send to useState
variable.
const onChangeHandler = e =>{
e.preventDefault();
setSearchCity(e.target.value);
}
useEffect(() => {
fetch(`http://api.openweathermap.org/data/2.5/weather?q=${searchCity}&units=metric&appid=sampleAPIKEYhere`)
.then((res) => res.json())
.then((data) => {
<form>
<input type="text" placeholder="Search a city" onChange={onChangeHandler}/>
</form>