This is my first react project, and I am having a problem passing input data to the weathermapAPI URL. I can console log the input but it doesn’t allow me to pass it to the URL. Here is the code:
import React, {Component, useState} from "react";
import Axios from 'axios'
import {GoSearch} from 'react-icons/go';
import styles from './ShowWeather.module.css'
import cloudSvg from '../images/cloud.svg'
function ShowWeather() {
const [city, setCity] = useState("")
const [weather, setWeather] = useState({})
const [error, setError] = useState(false)
const searchCity = async (e) => {
e.preventDefault()
console.log("--> " + city)
try {
const weatherResponse = await Axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${process.env.React_App_APIKEY}&lang=en`)
setWeather(weatherResponse)
} catch (e) {
console.log(e)
}
}
return (
<div>
<div className={styles.wrap}>
<form onSubmit={searchCity} className={styles.search}>
<input
className={styles.searchTerm}
type="text"
name="weatherCity"
placeholder="Search City"
onChange={(e) => setCity(e.target.value)}
required
/>
<button type="submit" className={styles.searchButton}>
<a><GoSearch/></a>
</button>
</form>
</div>
<div className={styles.weather_container}>
<div className={styles.inner_container}>
<div className={styles.location}>
<div><i className="fas fa-map-marker-alt"/></div>
<h3>New York</h3>
</div>
<div className={styles.weather_icon}>
<img src={cloudSvg} alt="cloud image"/>
</div>
<div className={styles.cloud_status}>
<h2>Clear</h2>
</div>
<div className={styles.temp}>
<h1>22.45°C</h1>
</div>
<div className={styles.wind_hum}>
<span><i className="fas fa-wind"/>WS: 1,24 km/s</span>
<span><i className="fas fa-tint"/>Humidity: 43%</span>
</div>
</div>
</div>
</div>
)
}
export default ShowWeather
Appreciate it if you could point out what I am forgetting