I want to toggle between Celcius and Fahrenheit with an onClick button on °C which can convert to Fahrenheit for my main.temp value max_temp and min_temp at the same time.
Weather.js
import React, { useState, useEffect } from 'react';
import Homepage from './Homepage';
function Weather() {
const API_KEY = 'b78ffacf63ad995ef34f6811b0e06433';
const [localweather, setLocalweather] = useState([]);
const [query, setQuery] = useState("");
//geolocation to get the local weather
const getLonLat = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const lon = position.coords.longitude;
const lat = position.coords.latitude;
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${API_KEY}`)
.then(response => response.json())
.then(result => {
setLocalweather(result)
return;
})
.catch(err => console.log(err));
})
}
}
useEffect(() => {
getLonLat();
}, [])
//give input to get other location weather
const searchInput = (e) => {
e.preventDefault()
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${query}&units=metric&appid=${API_KEY}`)
.then(response => response.json())
.then(result => {
setLocalweather(result)
setQuery("")
})
.catch(err => console.log(err));
}
return (
<Homepage
localweather={localweather}
getLonLat={getLonLat}
searchInput={searchInput}
setQuery={setQuery} />
)
}
export default Weather;
@Marmiz Sorry, I have deleted what I tried but I am facing the same problem even after trying your code. The temperature value that I get from the API shows undefined when I put it on the state.