I’m currently trying to create a weather website using weatherapi, but I’m running into a problem. If I log the object of location there’s no error, but if I try to log anything deeper than that object, like the name of the city it cannot read properties of undefined. If I comment out the log when it’s using the name of the city, then uncomment it again and don’t reload the page, then it will log the name of the city without error.
import React from 'react';
import './index.css';
import Navbar from "./components/Navbar"
import {useState} from "react"
import Weather from './components/Weather';
function App() {
const [inputData, setInputData] = useState({})
const [currentWeather, setCurrentWeather] = useState([])
const [loc, setLoc] = useState({loc:"Arlington"})
let apiKey = "7ab98e97faa64c4d8b4104902222202"
// console.log("Location: "+ loc.loc)
React.useEffect(() =>{///Finds weather data of certain location
console.log(loc.loc)
fetch(`https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${loc.loc}&aqi=no`)
.then(res => {
if(res.ok){
return res.json()
}
})
.then(data => {
if(data !=null){//Only change currentWeather when there is data for it
setCurrentWeather(data)
}else{
alert(`${loc.loc} was not found`)
}
})
}, [loc])
React.useEffect(() =>{///Finds locations with search bar
fetch(`https://api.weatherapi.com/v1/search.json?key=${apiKey}&q=${loc.loc}&aqi=no`)
.then(res => res.json())
.then(data => {
if(data.loc == null){
}else{
setLoc(data)
}
})
}, [])
//console.log(currentWeather.location.name)
return (
<div className="App">
<Navbar inputData={inputData} setLoc={setLoc} setInputData={setInputData}/>
<Weather currentWeather={currentWeather}/>
</div>
);
}
export default App;