Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

71
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda