Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

70
Views
Cannot assign fetch JSON nested object to useState in ReactJs

I am currently in learning the basics of ReactJs. I am trying to pass a nested object to the "weatherValues". After doing that in the fetchData function, it doesn't work and returns the original "null" value without passing the nested object from the fetch to the setState. Please help.

  function App() {
  const [dLocation, setDLocation] = useState('London');
  const [defaultLocation, setDefault] = useState(true);
  const [weatherValues, setWeatherValues] = **useState(null)**;

    const **fetchData** = () => {
    fetch(`${api.API_URL}?key=${api.API_KEY}&q=${dLocation}&days=${api.API_DAY_DATA}`)
    .then(response => response.json())
    .then(data => {
      **setWeatherValues(data)**
      //setWeatherValues({...weatherValues, ...data})
      //weatherArray = {...weatherArray, ...data};
      console.log(weatherValues)
      console.log(data)
    }
    );
    
}
 useEffect(() => {
        if(defaultLocation === true){
           **fetchData();**
           //console.log('Yes')
          // setWeatherValues(weatherArray)
           //console.log(weatherValues)
        }
        setDefault(false);
  })
  return (

   )
}

export default App;
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!