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

315
Vistas
ReactJS Error: Objects are not valid as a React child (found: [object Promise])

Having a nightmare trying to pull data from a weather API and display it in React but when I run my code I get an error:

"Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead."

My code is below:

export default function getWeatherFromApiAsync() {
  return fetch(
    "https://api.openweathermap.org/data/2.5/weather?q=brighton,uk&appid=8b609354454cdb6c5a7092a939861ace&units=metric"
  )
    .then((response) => response.json())
    .then((responseJson) => {
      return (
        <div className="App">
          {responseJson.map((weather) => (
            <div>
              <h6> {weather.coord.lon}</h6>
            </div>
          ))}
        </div>
      );
    })
    .catch((error) => {
      console.error(error);
    });
}

Any help appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't render a default component from promise like that.

You can take a advantages of useState , useEffect hook to fetch and render data into the component.

You can learn more about React Hooks from this link.

And also your api response is a json object and you're trying to map over it.

Note: You can map over array type of data only.

Here is your working code of yours,

import { useEffect, useState } from "react";

const App = () => {
  const [data, setData] = useState({});
  useEffect(() => {
    const getWeatherFromApiAsync = async () => {
      const resopnse = await fetch(
        "https://api.openweathermap.org/data/2.5/weather?q=brighton,uk&appid=8b609354454cdb6c5a7092a939861ace&units=metric"
      );
      const resopnseJson = await resopnse.json();
      console.log("json", resopnseJson);
      setData(resopnseJson);
    };
    getWeatherFromApiAsync();
  }, []);

  return (
    <div className="App">
      <h6>{data?.coord?.lon}</h6>
    </div>
  );
};

export default App;

about 4 years ago · Juan Pablo Isaza Denunciar
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